使用Android中的JSONObject和JSONArray解析json数据

1、代码

public class MainActivity extends Activity {TextView tv_json;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tv_json = (TextView) findViewById(R.id.tv_json);JSONObject jsonObject = new JSONObject();//往里面put数据JSONArray ja = new JSONArray();//将JsonObject对象放入JSONObject jsonObject2 = new JSONObject();//往里面put数据JSONArray ja2 = new JSONArray();//将JsonObject对象放入JSONObject jsonInfo = new JSONObject();//存放jsonArray数据 实现key-valuetry {jsonObject.put("name", "tom");jsonObject.put("password", "123");jsonObject.put("sex", "man");jsonObject.put("age", "20");jsonObject2.put("name2", "tom");jsonObject2.put("password2", "123");jsonObject2.put("sex2", "man");jsonObject2.put("age2", "20");ja.put(jsonObject);ja2.put(jsonObject2);jsonInfo.put("first", ja);jsonInfo.put("second", ja2); System.out.println("jsoninfo==="+jsonInfo.toString());tv_json.setText("sum===" + jsonInfo.toString());JSONObject getJson = new JSONObject(jsonInfo.toString());JSONArray jArray=(JSONArray) getJson.get("first");for(int i=0;i<jArray.length();i++){JSONObject o=(JSONObject) jArray.get(i);System.out.println("o.name==="+o.getString("name"));}} catch (JSONException e) {e.printStackTrace();}}}

2、输出结果:

jsoninfo==={"second":[{"age2":"20","sex2":"man","name2":"tom","password2":"123"}],"first":[{"password":"123","sex":"man","age":"20","name":"tom"}]}

o.name===tom

2.2、json数据 ,,无序

{ "second": [ { "age2": "20", "sex2": "man", "name2": "tom", "password2": "123" } ], "first": [ { "password": "123", "sex": "man", "age": "20", "name": "tom" } ]}

别人失去了信心,他却下决心实现自己的目标。

使用Android中的JSONObject和JSONArray解析json数据

相关文章:

你感兴趣的文章:

标签云: