1. package sn.len.json;
  2. import org.json.JSONArray;
  3. import org.json.JSONException;
  4. import org.json.JSONObject;
  5. import android.app.Activity;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. public classJSONActivity extends Activity {
  9. privateString jsondata;
  10. @Override
  11. publicvoid onCreate(Bundle savedInstanceState)
  12. {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.main);
  15. try
  16. {
  17. //调用构建JSON字符串方法
  18. buildJson();
  19. }
  20. catch(JSONException e)
  21. {
  22. e.printStackTrace();
  23. }
  24. }
  25. //构建JSON字符串
  26. publicvoid buildJson() throwsJSONException
  27. {
  28. JSONArrayjson=new JSONArray();
  29. JSONObjectjsonObj=new JSONObject();
  30. for(inti=0;i<2;i++)
  31. {
  32. jsonObj.put("id","001");
  33. jsonObj.put("age","20");
  34. jsonObj.put("name","snoanw");
  35. //把每个数据当作一对象添加到数组里
  36. json.put(jsonObj);
  37. }
  38. jsondata=json.toString();
  39. Log.i("JSON",jsondata);
  40. //调用解析JSON方法
  41. parserJson(jsondata);
  42. }
  43. // 解析JSON字符串
  44. publicvoid parserJson(String jsondata) throwsJSONException
  45. {
  46. //构建JSON数组对象
  47. JSONArrayjson1=new JSONArray(jsondata);
  48. for(inti=0;i<json1.length();i++)
  49. {
  50. JSONObjectjsonObj2=json1.optJSONObject(i);
  51. Stringid=jsonObj2.getString("id");
  52. Stringage=jsonObj2.getString("age");
  53. Stringname=jsonObj2.getString("name");
  54. Log.i("JSONDATA",id+age+name);
  55. }
  56. }
  57. }
package sn.len.json; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.os.Bundle; import android.util.Log; public class JSONActivity extends Activity { private String jsondata; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); try { //调用构建JSON字符串方法 buildJson(); } catch (JSONException e) { e.printStackTrace(); } } //构建JSON字符串 public void buildJson() throws JSONException { JSONArray json=new JSONArray(); JSONObject jsonObj=new JSONObject(); for(int i=0;i<2;i++) { jsonObj.put("id", "001"); jsonObj.put("age", "20"); jsonObj.put("name", "snoanw"); //把每个数据当作一对象添加到数组里 json.put(jsonObj); } jsondata=json.toString(); Log.i("JSON", jsondata); //调用解析JSON方法 parserJson(jsondata); } // 解析JSON字符串 public void parserJson(String jsondata) throws JSONException { //构建JSON数组对象 JSONArray json1=new JSONArray(jsondata); for(int i=0;i<json1.length();i++) { JSONObject jsonObj2=json1.optJSONObject(i); String id=jsonObj2.getString("id"); String age=jsonObj2.getString("age"); String name=jsonObj2.getString("name"); Log.i("JSONDATA", id+age+name); } } } 构建JSON字符串

JSON字符串解析和构建 ." alt="" src="https://img.it610.com/image/info5/30bae50bf7534f75a6d11b5ae6e9a62a.jpg" width="650" height="47">

//解析JSON字符串

JSON字符串解析和构建 ." alt="" src="https://img.it610.com/image/info5/bddaba82b4154818a60e8e14d812c620.jpg" width="412" height="81">

更多相关文章

  1. Android实现DES对字符串加密
  2. android使用pull解析器读写xml文件…
  3. XML构建Menu Android
  4. Android腾讯微博开发之随机字符串与签名实现
  5. Android(安卓)Xml解析
  6. std::to_string is not memember of std android
  7. android 字符串国际化的问题
  8. Android(安卓)中数据库查询方法 query() 中的参数
  9. Android(安卓)Studio:为Android定制的IDE

随机推荐

  1. android ndk native_activity.h
  2. Android实现CoverFlow效果
  3. Android(安卓)v2.0 基本概念 - 浅谈
  4. android 手机定位
  5. android5 sdk 下载地址大全
  6. Android(安卓)长按识别图中二维码
  7. “android studio 运行程序提示Applicati
  8. android中textview设置为多行文本时,如何
  9. Android(安卓)Initialization Process --
  10. Android(安卓)Wear Preview- 创建通知(Cre