아하
검색 이미지
생활꿀팁 이미지
생활꿀팁생활
생활꿀팁 이미지
생활꿀팁생활
내추럴한아나콘다73
내추럴한아나콘다7320.08.12

안드로이드 스튜디오 게시판 기능 구현 코드

TextView ant;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

tv=findViewById(R.id.ant);

}

public void clickBtn(View view) {

try {

InputStream is= assetManager.open("jsons/an3172.json");

InputStreamReader isr= new InputStreamReader(is);

BufferedReader reader= new BufferedReader(isr);

StringBuffer buffer= new StringBuffer();

String line= reader.readLine();

while (line!=null){

buffer.append(line+"\n");

line=reader.readLine();

}

String jsonData= buffer.toString();

JSONArray jsonArray= new JSONArray(jsonData);

String s="";

String name= jo.getString("name");

String msg= jo.getString("msg");

JSONObject flag=jo.getJSONObject("flag");

int a= flag.getInt("a");

int b= flag.getInt("b");

s += name+" : "+msg+"==>"+a+","+b+"\n";

}

tv.setText(s);

} catch (IOException e) {e.printStackTrace();} catch (JSONException e) {e.printStackTrace(); }

이게 자바 클래스에서 구현한 json의 예시본인데 만약 s += name+" : "+msg+"==>"+a+","+b+"\n";이것을 어떻게 해야지 2줄의 배열로 나타낼수있을까요?

[

{"name": "sam","msg": "Hello world", "flag": {a": 10, "b": 20}},

{"name": "robin","msg": "Nice to meet you", "flag": {"a": 100,"b": 200}}

]

Json코드입니다. 저렇게 되어있는 부분을 두줄로 불러드릴려면 s += name+" : "+msg+"==>"+a+","+b+"\n"; 이코드를 어떤식으로 변형을해야지 json을 불러들었을때 한줄이아닌 두줄로 나타낼수있을까요?


예제의 출처:https://lcw126.tistory.com/m/101

55글자 더 채워주세요.
답변의 개수1개의 답변이 있어요!
  • 안녕하세요.

    링크는 json을 읽어서 textview에 출력하는 예제같습니다.

    그리고 질문 주신 부분은 그 text에 출력을 두줄로 하고 싶으신것 같구요,

    s += name+" : "+msg+"==>"+a+","+b+"\n";

    부분에서 두번째 줄의 처음에 출력되는 자리에

    "\n"+

    를 줄이 바뀌게 됩니다.

    하지만 보통은 각각의 필드에 별도의 textbox를 만들어서 각각의 textbox에 값을 넣게 됩니다. 그래야 formatting이 쉬워지기 때문입니다.

    감사합니다.