当前位置: 代码迷 >> JavaScript >> JSON数据的简略解析
  详细解决方案

JSON数据的简略解析

热度:115   发布时间:2012-08-09 15:59:21.0
JSON数据的简单解析

运行结果:

?

?

?

--------------------------------------

//解析方法
	public void parseJson(String jsonData) {
		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("name")) {
						System.out.print("name=" + reader.nextString() + "  ");
					} else if (tagName.equals("age")) {
						System.out.println("age=" + reader.nextInt());
					}
				}
				reader.endObject();
			}
			reader.endObject();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

===============================================
public class Main extends Activity {
	/** Called when the activity is first created. */
	private String jsonData = "[{\"name\":\"Jack\",\"age\":20},{\"name\":\"Tom\",\"age\":25}]";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		JsonUtils jsonUtils = new JsonUtils();
		jsonUtils.parseJson(jsonData);

	}
}

?

  相关解决方案