Android解析json数据

Json数据

[{"code":"110000","sheng":"11","di":"00","xian":"00","name":"北京市","level":1},
{"code":"659004","sheng":"65","di":"90","xian":"04","name":"五家渠市","level":3},
{"code":"710000","sheng":"71","di":"00","xian":"00","name":"台湾省","level":1},
{"code":"810000","sheng":"81","di":"00","xian":"00","name":"香港特别行政区","level":1},
{"code":"820000","sheng":"82","di":"00","xian":"00","name":"澳门特别行政区","level":1}]

  这里附上一个检查json数据格式是否正确的网站http://www.json.cn/

三种解析方式android自带解析,Gson解析,FastJson解析

 

1、android自带解析

 

        json_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = getString();
                Log.d("txt",s);
                List list = new ArrayList();
                try {
                    JSONArray obj = new JSONArray(s);
                    for(int i=0;i){
                        JSONObject jsonobj = (JSONObject)obj.get(i);
                        City city = new City();
                        city.setCode(jsonobj.getString("code"));
                        city.setSheng(jsonobj.optString("sheng"));
                        city.setDi(jsonobj.optString("di"));
                        city.setXian(jsonobj.optString("xian"));
                        city.setName(jsonobj.optString("name"));
                        city.setLevel(jsonobj.optInt("level"));
                        Log.d("txt",city.toString());
                        textView.append(city.toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });

  先获取JSON数组,在解析JsonObject。

2、Gson解析

 Gson_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = getString();
                JsonParser parser = new JsonParser();
                JsonArray jsonArray = parser.parse(s).getAsJsonArray();
                Gson gson = new Gson();
                for (JsonElement obj:jsonArray){
                    City city = gson.fromJson(obj,City.class);
                    Log.d("txt", city.toString());
                    textView.append(city.toString());
                }


            }
        });
这样也可以
  Gson_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = getString();
City[] lists = new Gson().fromJson(s, City[].class);
for(City city:lists){
Log.d("txt",city.toString());
}
}
});

这样也可以
son_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = getString();
List lists = new ArrayList();
Type type = new TypeToken>(){}.getType();
Gson gson = new Gson();
lists = gson.fromJson(s,type);
for(City city:lists){
Log.d("txt",city.toString());
}
}
});
 

  先获取Json数组,再解析jsonObject。

3、FastJson

 fastjson_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = getString();
                Log.d("txt",s);
                List lists = JSON.parseArray(s,City.class);

                for(City city:lists){
                    textView.append(city.toString());
                    Log.d("txt",city.toString());
                }
            }
        });

  直接获取JsonArray解析List

  public static final Object parse(String text); // 把JSON文本parse为JSONObject或者JSONArray 

    public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject 

    public static final T parseObject(String text, Class clazz); // 把JSON文本parse为JavaBean 

    public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray

  public static final List parseArray(String text, Class clazz); //把JSON文本parse成JavaBean集合 

    public static final String toJSONString(Object object); // 将JavaBean序列化为JSON文本 

    public static final String toJSONString(Object object, boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本 

    public static final Object toJSON(Object javaObject); 将JavaBean转换为JSONObject或者JSONArray(和上面方法的区别是返回值是不一样的)

 

  三种解析方法,个人感觉fastjson使用起来还是很方便的。

转载于:https://www.cnblogs.com/yrstudy/p/6596540.html

你可能感兴趣的:(Android解析json数据)