나는 하루의 더 나은 부분을 위해 GSON과 싸워왔다. 글쎄, 안드로이드에 대한 목록의 나의 일반적인 비 이해만큼 나는 GSON만큼 생각하지 않는다. 기본적으로 무엇을 내가 각이 목록의 포맷 및 표시됩니다 JSON 2 개 배열, 수신하고있다 일어나고있다 : 나는 잘 개별 문자열을 읽을 수있어안드로이드 JSON 파싱 배열에 GSON
Adventures:
+-------------+
| Adventure 1 |
+-------------+
| Adventure 2 |
+-------------+
Events:
+-------------+
| Event 1 |
+-------------+
| Event 2 |
+-------------+
을, 그러나 나는받지 못했습니다 이것을 배열로 옮기는 내 머리. 내가 잡있어 JSON 형식은 다음과 같습니다
{
"events": [
{
"eid": "11111111",
"bid": "aaaaaaaa",
"bname": "Example Business 1",
"start": "3/26/14 @ 6pm",
"end": "3/27/14 @ 2am",
"points": "50",
"title": "Example Event 1",
"description": "Example Event Description",
"cat": "Nightlife",
"type": "Bar",
"subtype": "Karaoke",
"valid": true
},
{
"eid": "22222222",
"bid": "bbbbbbbb",
"bname": "Example Business 2",
"start": "3/26/14 @ 6pm",
"end": "3/27/14 @ 2am",
"points": "50",
"title": "Example Event 2",
"description": "Example Event Description",
"cat": "Nightlife",
"type": "Comedy",
"subtype": "General",
"valid": true
},
{
"eid": "33333333",
"bid": "cccccccc",
"bname": "Example Business 3",
"start": "3/26/14 @ 6pm",
"end": "3/27/14 @ 2am",
"points": "150",
"title": "Example Event 3",
"description": "Example Event Description",
"cat": "Dining",
"type": "Restraunt",
"subtype": "Chinese",
"valid": true
}
],
"adventures": [
{
"aid": "11111111",
"bid": "aaaaaaaa",
"start": "3/26/14 6pm",
"end": "3/27/14 2am",
"points": "150",
"title": "Example Adventure 1",
"description": "Example Adventure Description",
"cat": "Nightlife",
"type": "Bar",
"subtype": "Karaoke",
"steps_comp": "2",
"total_steps": "5",
"valid": true
},
{
"aid": "22222222",
"bid": "bbbbbbbb",
"start": "3/26/14 6pm",
"end": "3/27/14 2am",
"points": "250",
"title": "Example Adventure 2",
"description": "Example Adventure Description",
"cat": "Nightlife",
"type": "Bar",
"subtype": "Neighborhood",
"steps_comp": "0",
"total_steps": "5",
"valid": true
}
]
} 나는 다음과 JSON을 구문 분석하고있어
(발리 코드 포함) :
RequestQueue queue = Volley.newRequestQueue(this);
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, full_url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
String example = response.toString();
Log.e("JSON Response", example);
try {
JsonParser jsonParser = new JsonParser();
JsonObject jo = (JsonObject)jsonParser.parse(example);
JsonArray jsonArr = jo.getAsJsonArray("events");
//jsonArr.
Gson googleJson = new Gson();
ArrayList jsonObjList = googleJson.fromJson(jsonArr, ArrayList.class);
System.out.println("List size is : "+jsonObjList.size());
System.out.println("List Elements are : "+jsonObjList.toString());
for (int i = 0; i < jsonObjList.size(); i++){
String item = jsonObjList.get(i).toString();
System.out.println("Item " + i + " : " + item);
}
Toast.makeText(getApplicationContext(), "0: "+jsonObjList.get(0),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "blah",
Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
}
);
queue.add(jsObjRequest);
내가 각 행을 단지 받고 있어요 괜찮아요.하지만 그것은 단지 하나의 문자열 일 뿐이므로 각 요소에 대한 개별 요소를 얻을 수는 없습니다. 나는 for 루프 안에서 또 다른 GSON 호출을해야 할 필요가 있다고 생각하고있다. 이것은 지저분 해 보인다. 나는 약간의 googleing을 해왔다. (나는 이걸 어떻게 얻었 는가?)하지만이 작품들을 만들기 위해 조각들을 모아 놓을 수는 없다. 어떤 도움을 주시면 감사하겠습니다.
그 일은 챔피언을 좋아했습니다. 고맙습니다. –
@NativeNerd, 기꺼이 도와 드리겠습니다. –