JSON解析 Gson解析方式 JSONObject JSONArray
来源:互联网 发布:nginx自定义错误页面 编辑:程序博客网 时间:2024/06/11 18:49
直接上代码
主程序代码:
package com.cc.mygson;import java.net.Proxy.Type;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import com.google.gson.Gson;import com.google.gson.reflect.TypeToken;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;public class MainActivity extends Activity{String json = "{\"total\":100,\"rows\":[{\"key\":\"key1\",\"value\":\"value1\"},{\"key\":\"key2\",\"value\":\"value2\"}]}";private Gson gson; @Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.layotu);gson = new Gson();findViewById(R.id.btn).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {gsonMyJSONType();}});}/** *基础JSON解析方法 */private void gsonJson() {try {JSONObject jsonObject = new JSONObject(json);int total = jsonObject.getInt("total");JSONArray array = jsonObject.getJSONArray("rows");for(int i = 0 ;i<array.length();i++){String key = array.getJSONObject(i).getString("key");String value = array.getJSONObject(i).getString("value");}} catch (JSONException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}/** *一般Gson解析方法 */private void gsonMyJSON(){MyJson myJson = gson.fromJson(json, MyJson.class);int total = myJson.getTotal();}/** * 一般Type Gson解析方法 */private void gsonMyJSONType(){java.lang.reflect.Type type = new TypeToken<MyJson>(){}.getType();MyJson myJson = gson.fromJson(json, type);int total = myJson.getTotal();}}
package com.cc.mygson;import java.util.ArrayList;public class MyJson {private int total; private ArrayList<Rows> rows = new ArrayList<Rows>();public int getTotal() {return total;}public void setTotal(int total) {this.total = total;}public ArrayList<Rows> getRows() {return rows;}public void setRows(ArrayList<Rows> rows) {this.rows = rows;}}
Rows类内容为下:
package com.cc.mygson;public class Rows {private String key ;private String value ;public String getKey() {return key;}public void setKey(String key) {this.key = key;}public String getValue() {return value;}public void setValue(String value) {this.value = value;}}
0 0
- JSON解析 Gson解析方式 JSONObject JSONArray
- JSONObject/JSONArray解析json
- jsonObject和jsonArray解析json的方式
- jsonObject和jsonArray解析json的方式
- 解析JSON-JSONObject/GSON
- JSON的三种解析方式以及JSONObject、JSONArray区别
- Android中Json的全解析(JSONObject,JSONArray,Gson,Jackson)
- JSONObject与JSONArray解析Json数据例子
- Android JSON 解析(一) JSONObject 和 JSONArray
- JSONObject与JSONArray的使用-解析Json
- Android JSON解析(JSONObject和JSONArray)
- JSON数据解析(JSONArray和JSONObject)
- 使用JSONObject和JSONArray 进行Json解析
- Android JSON解析(JSONObject和JSONArray)
- 用JSONObject和JSONArray 解析json数据
- 使用JSONObject解析json串,JSONArray解析json
- JsonObject和JsonArray解析
- android JSON解析之JSONObject与GSON
- SQL表连接查询(inner join、full join、left join、right join)【转载】
- Codeforces Round #325 (Div. 2) A
- 差分约束系统
- 2015/10/13
- Codeforces Round #325 (Div. 2) B
- JSON解析 Gson解析方式 JSONObject JSONArray
- 复习javascript基础 (1)
- Foundation Kit框架概叙
- 初步18-画星星
- 五个解决方案让MongoDB拥有RDBMS的鲁棒性事务
- Promise/deferred(转)
- HDU 5500 Reorder the Books
- javascript base库
- 写在开发前——android各种准备工作