- 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 classJSONActivity extends Activity {
- privateString jsondata;
- @Override
- publicvoid onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- try
- {
- //调用构建JSON字符串方法
- buildJson();
- }
- catch(JSONException e)
- {
- e.printStackTrace();
- }
- }
- //构建JSON字符串
- publicvoid buildJson() throwsJSONException
- {
- JSONArrayjson=new JSONArray();
- JSONObjectjsonObj=new JSONObject();
- for(inti=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字符串
- publicvoid parserJson(String jsondata) throwsJSONException
- {
- //构建JSON数组对象
- JSONArrayjson1=new JSONArray(jsondata);
- for(inti=0;i<json1.length();i++)
- {
- JSONObjectjsonObj2=json1.optJSONObject(i);
- Stringid=jsonObj2.getString("id");
- Stringage=jsonObj2.getString("age");
- Stringname=jsonObj2.getString("name");
- Log.i("JSONDATA",id+age+name);
- }
- }
- }
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">
- Android实现DES对字符串加密
- android使用pull解析器读写xml文件…
- XML构建Menu Android
- Android腾讯微博开发之随机字符串与签名实现
- Android(安卓)Xml解析
- std::to_string is not memember of std android
- android 字符串国际化的问题
- Android(安卓)中数据库查询方法 query() 中的参数
- Android(安卓)Studio:为Android定制的IDE
随机推荐
-
android ndk native_activity.h
-
Android实现CoverFlow效果
-
Android(安卓)v2.0 基本概念 - 浅谈
-
android 手机定位
-
android5 sdk 下载地址大全
-
Android(安卓)长按识别图中二维码
-
“android studio 运行程序提示Applicati
-
android中textview设置为多行文本时,如何
-
Android(安卓)Initialization Process --
-
Android(安卓)Wear Preview- 创建通知(Cre