日期:2014-05-16 浏览次数:20454 次
跟XML类似,JsonReader也可以解析文件中的JSon数据,下面的例子是从帮忙文档中取下来的
首先创建一个《JsonFile.txt》文件,JSon数据如下
[ { "id": 912345678901, "text": "How do I read JSON on Android?", "geo": null, "user": { "name": "android_newb", "followers_count": 41 } }, { "id": 912345678902, "text": "@android_newb just use android.util.JsonReader!", "geo": [50.454722, -104.606667], "user": { "name": "jesse", "followers_count": 2 } } ]
package com.example.jsondemo; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.os.Environment; import android.os.Message; import android.app.Activity; import android.util.JsonReader; import android.util.JsonToken; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private Button btn = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new btnListener()); } private class btnListener implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub String path = Environment.getExternalStorageDirectory() + File.separator + "JsonFile.txt"; System.out.println("-----------btnListener---------" + path); try { readJsonStream(new FileInputStream(path)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public List<Message> readJsonStream(InputStream in) throws IOException { JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8")); try { return readMessagesArray(reader); } finally { reader.close(); } } public List<Message> readMessagesArray(JsonReader reader) throws IOException { List<Message> messages = new ArrayList<Message>(); reader.beginArray(); while (reader.hasNext()) { messages.add(readMessage(reader)); } reader.endArray(); return messages; } public Message readMessage(JsonReader reader) throws IOException { long id = -1; String text = null; User user = null; List geo = null; reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("id")) { id = reader.nextLong(); System.out.println("id = " + id); } else if (name.equals("text")) { text = reader.nextString(); System.out.println("text = " + text); } else if (name.equals("geo") && reader.peek() != JsonToken.NULL) { geo = readDoublesArray(reader); System.out.println("geo = " + geo); } else if (name.equals("user")) { user = readUser(reader); System.out.println("user = " + user); } else { reader.skipValue(); } } reader.endObject(); return new Message(); } public List<Double> readDoublesArray(JsonReader reader) throws IOException { List<Double> doubles = new ArrayList<Double>(); reader.beginArray(); while (reader.hasNext()) { doubles.add(reader.nextDouble()); } reader.endArray(); return doubles; } public User