android jsonreader
what's json----> [{"name":"leeprison","age":"18"},{"name":"leeprison1","age":"19"}]
what's jsonarray--> 像上面的就有两个数组
what's jsonobject->就是上面的键值对,一个name对应着一个string
JsonReader
String jsonData = "[{\"username\":\"LeePrison \",\"userId\":\"001\"},{\"username\":\"LeePrison\",\"userId\":\"002\"}]"; try{ JsonReader reader = new JsonReader(new StringReader(jsonData)); reader.beginArray(); while(reader.hasNext()){ reader.beginObject(); while(reader.hasNext()){ String tagName = reader.nextName(); if(tagName.equals("username")){ System.out.println(reader.nextString()); } else if(tagName.equals("userId")){ System.out.println(reader.nextString()); } } reader.endObject(); } reader.endArray(); } catch(Exception e){ e.printStackTrace(); }
也有简单的,android对应不同复杂度的json数据类型提供不同的解析方式,其实还是分成jsonobject jsonarray
type_one---》["name":"lee","age":"18"]
JsonObject
JSONObject jsonObject = new JSONObject(json); String name = jsonObject.getString("name"); String age=jsonObject.getString("age");
type_two--->[{"name":"leeprison","age":"18"},{"name":"leeprison1","age":"19"}] 这种可以用jsonReader也可以下面的方式
jsonArray
try { JSONArray jsonArray = new JSONArray(string); for (int i = 0; i < jsonArray.length(); i++) { jsonObject = jsonArray.getJSONObject(i); String name = jsonObject.getString("name"); int age = jsonObject.getInt("age"); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
对于比较复杂的就是用list来读了~再说了