zoukankan      html  css  js  c++  java
  • Android 中解析 JSON

    有什么不懂的可以去官网去看看:www.json.org

    在google android中也有关于解析JSON的类库:JsonReader,但是只能在3.0以后的版本中才可以用,在这里我们用google提供的类库google-gson,可以从code.google.com/p/google-gson/下载jar包。

    下面通过一个小例子来学习一下:

    例子:

    [{"name":"zhangsan","age":22},{"name":"lisi","age":23}]

    分析:

    1.开始解析数组

    2.开始解析对象

    3.解析键值对

    4.解析键值对

    5.解析对象结束

    6.开始解析对象

    7.解析键值对

    8.解析键值对

    9.解析对象结束

    10.解析数组结束

    下面的是一个Activity,很简单只有一个button,并为button添加单击事件,

    01 package com.tony.json;
    02   
    03 import android.app.Activity;
    04 import android.os.Bundle;
    05 import android.view.View;
    06 import android.widget.Button;
    07   
    08 public class JsonActivity extends Activity {
    09     /** Called when the activity is first created. */
    10       
    11     private String jsonData = "[{"name":"zhangsan","age":22},{"name":"lisi","age":23}]";
    12     private Button jsonButton;
    13     @Override
    14     public void onCreate(Bundle savedInstanceState) {
    15         super.onCreate(savedInstanceState);
    16         setContentView(R.layout.main);
    17         jsonButton = (Button) findViewById(R.id.json_button);
    18         jsonButton.setOnClickListener(new View.OnClickListener() {
    19               
    20             @Override
    21             public void onClick(View v) {
    22                 JsonUtils jsonUtils = new JsonUtils();
    23                 jsonUtils.parseJson(jsonData);
    24             }
    25         });
    26     }
    27 }

    这个类中主要是解析json数组:

    01 package com.tony.json;
    02   
    03 import java.io.IOException;
    04 import java.io.StringReader;
    05   
    06 import android.util.Log;
    07   
    08 import com.google.gson.stream.JsonReader;
    09   
    10 public class JsonUtils {
    11     private static final String TAG = "JsonUtils";
    12       
    13     public void parseJson(String jsonData){
    14         JsonReader reader = new JsonReader(new StringReader(jsonData));
    15         try {
    16             reader.beginArray();    // 开始解析数组
    17             while (reader.hasNext()) {
    18                 reader.beginObject();   // 开始解析对象
    19                 while (reader.hasNext()) {
    20                     String tagName = reader.nextName(); // 得到键值对中的key
    21                     if (tagName.equals("name")) {   // key为name时
    22                         Log.i(TAG, "name--------->" + reader.nextString());  // 得到key中的内容
    23                     }else if (tagName.equals("age")) {  // key为age时
    24                         Log.i(TAG, "age--------->" + reader.nextInt());  // 得到key中的内容
    25                     }
    26                 }
    27                 reader.endObject();
    28             }
    29             reader.endArray();
    30         } catch (IOException e) {
    31             e.printStackTrace();
    32         }
    33     }
    34 }

    下面是运行后在logcat中打印出的结果:Android 中解析 JSON

  • 相关阅读:
    lintcode254- Drop Eggs- easy
    lintcode462- Total Occurrence of Target- easy
    lintcode63- Search in Rotated Sorted Array II- medium
    lintcode62- Search in Rotated Sorted Array- medium
    lintcode74- First Bad Version- medium
    lintcode75- Find Peak Element- medium
    lintcode160- Find Minimum in Rotated Sorted Array II- medium
    lintcode159- Find Minimum in Rotated Sorted Array- medium
    lintcode447- Search in a Big Sorted Array- medium
    VS2005、vs2008+WinXPDDK+DDKWizard配置驱动开发环境 分类: VC++ 2015-01-05 09:38 448人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/jianrong-zheng/p/3217242.html
Copyright © 2011-2022 走看看