最近学习了googleAPI中自带的一个解析和生成json的API
jsonReader:
需要解析的json如下:
{
"id": 912345678901,
"text": "How do I read JSON on Android?",
"geo": [50.454722, -104.606667],
"user": {
"name": "android_newb",
"followers_count": 41
}
解析的代码如下:
InputStream is = null; try { is = this.getAssets().open("test.json", Context.MODE_PRIVATE); int length = is.available(); byte[] buffer = new byte[length]; is.read(buffer); String temp = new String(buffer); Reader response = new StringReader(temp.toString()); // parseResponse(response); reader = new JsonReader(response); reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if ("id".equals(name)) { String id = reader.nextString(); System.out.println(id); }else if ("text".equals(name)) { String text = reader.nextString(); System.out.println(text); }else if("geo".equals(name) && reader.peek() != JsonToken.NULL){ List<Double> doubles = new ArrayList<Double>(); reader.beginArray(); while (reader.hasNext() ) { doubles.add(reader.nextDouble()); } reader.endArray(); for (Object object : doubles) { System.out.println("doubles======="+object); } }else if ("user".equals(name)) { reader.beginObject(); while (reader.hasNext()) { name = reader.nextName(); if ("name".equals(name)) { String name2 = reader.nextString(); System.out.println(name2); }else if ("followers_count".equals(name)) { int id = reader.nextInt(); System.out.println(id); } } reader.endObject(); } } } catch (IOException ex) { ex.printStackTrace(); }finally{ if (null != reader) { reader.close(); } }
{
"id" : "3232",
"data" : [{
"data1" : "555",
"data2" : "3243"
},
{
"data1" : "888",
"data2" : "777"
}]
}
解析的代码如下:
JsonReader reader = new JsonReader(response); reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if ("id".equals(name)) { String id = reader.nextString(); System.out.println("===id="+id); } else if (name.equals("data")) { reader.beginArray(); while (reader.hasNext()) { reader.beginObject(); String name1; while (reader.hasNext()) { name1 = reader.nextName(); if (name1.equals("data1")) { String s1 = reader.nextString(); System.out.println("===s1="+s1); } else if (name1.equals("data2")) { String s2 = reader.nextString(); System.out.println("===s2="+s2); } else { reader.skipValue(); } } reader.endObject(); } reader.endArray(); } else { reader.skipValue(); } } reader.endObject(); reader.close(); }
try { writer = new JsonWriter(new OutputStreamWriter(new FileOutputStream(new File("/data/data/com.example.jsonreadertest/test.json")))); writer.beginObject(); writer.name("id").value(2); writer.name("name").value("zhangsan"); writer.name("text").value("How do I write JSON on Android?"); writer.name("geo"); writer.beginArray(); writer.value(25.0f); writer.value(33.0f); writer.endArray(); writer.name("user"); writer.beginObject(); writer.name("ming").value("xiaowang"); writer.name("followers_count").value(41); writer.endObject(); writer.endObject(); writer.flush(); } catch (Exception e) { e.printStackTrace(); }finally{ try { writer.close(); } catch (IOException e) { e.printStackTrace(); } }