Parsing JSON-formatted data in Android

Source: Internet
Author: User

This article from http://tonysun3544.iteye.com/category/188238

Package Com.tony.json;

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button;

public class Jsonactivity extends Activity {
/* Called when the activity is first created. /

private String jsonData = "[{\"name\":\"zhangsan\",\"age\":22},{\"name\":\"lisi\",\"age\":23}]";  private Button jsonButton;  @Override  public void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentView(R.layout.main);      jsonButton = (Button) findViewById(R.id.json_button);      jsonButton.setOnClickListener(new View.OnClickListener() {          @Override          public void onClick(View v) {              JsonUtils jsonUtils = new JsonUtils();              jsonUtils.parseJson(jsonData);          }      });  }  

}

The main parsing of this class is the JSON array:

Java Code Collection Code
Package Com.tony.json;

Import java.io.IOException;
Import Java.io.StringReader;

Import Android.util.Log;

Import Com.google.gson.stream.JsonReader;

public class Jsonutils {
private static final String TAG = "Jsonutils";

public void parseJson(String jsonData){      JsonReader reader = new JsonReader(new StringReader(jsonData));      try {          reader.beginArray();    // 开始解析数组          while (reader.hasNext()) {              reader.beginObject();   // 开始解析对象              while (reader.hasNext()) {                  String tagName = reader.nextName(); // 得到键值对中的key                  if (tagName.equals("name")) {   // key为name时                      Log.i(TAG, "name--------->" + reader.nextString());  // 得到key中的内容                  }else if (tagName.equals("age")) {  // key为age时                      Log.i(TAG, "age--------->" + reader.nextInt());  // 得到key中的内容                  }              }              reader.endObject();          }          reader.endArray();      } catch (IOException e) {          e.printStackTrace();      }  }  

}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Parsing JSON-formatted data in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.