JSON解析数组(包含其他)
来源:互联网 发布:网络同志电视剧有哪些 编辑:程序博客网 时间:2024/06/17 04:40
在AndroidManifest.xml中配置权限:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.li.json"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MyJSONDemo"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
在main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:gravity="center_horizontal"
android:layout_marginTop="8dp"
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
在MyJSONDemo.java中:
package com.li.json;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.database.CursorJoiner.Result;
import android.os.Bundle;
import android.widget.TextView;
public class MyJSONDemo extends Activity {
private TextView text = null ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.text = (TextView) super.findViewById(R.id.text) ;
String str = "{\"menberdata\":[{\"id\":1,\"name\":\"李叶文\",\"age\":30},"
+ "{\"id\":2,\"name\":\"HCXY\",\"age\":10}],\"company\":\"河池学院\"}";
StringBuffer buf = new StringBuffer() ;
try {
Map<String,Object> result = this.parseJson(str); //解析文本
buf.append("学校名称:" + result.get("company") + "\n");
@SuppressWarnings("unchecked")
List<Map<String,Object>> all = (List<Map<String,Object>>)result.get("menberdata");
Iterator<Map<String,Object>> iter = all.iterator() ;
while(iter.hasNext()){
Map<String,Object> map = iter.next() ;
buf.append("ID:" + map.get("id") + ",姓名:" + map.get("name")
+ ",年龄:" + map.get("age") + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
this.text.setText(buf) ;
}
private Map<String, Object> parseJson(String data) throws Exception {
Map<String, Object> allmMap = new HashMap<String, Object>();
JSONObject allData = new JSONObject(data); //全部的内容变为一个项
allmMap.put("company",allData.getString("company")); //取出项
JSONArray jsonArr = allData.getJSONArray("menberdata"); //取出数组
List<Map<String,Object>> all = new ArrayList<Map<String,Object>>();
for (int x = 0; x < jsonArr.length(); x++) {
Map<String, Object> map = new HashMap<String, Object>();
JSONObject jsonObj = jsonArr.getJSONObject(x);
map.put("id", jsonObj.getInt("id"));
map.put("name", jsonObj.getString("name"));
map.put("age", jsonObj.getInt("age"));
all.add(map);
}
allmMap.put("menberdata",all);
return allmMap;
}
}
- JSON解析数组(包含其他)
- JAVA中快速解析JSON对象里包含的JSON数组
- JAVA中快速解析JSON对象里包含的JSON数组
- 解析数组里面包含数组的json格式的数据代码
- 解析JSON问题(一)包含特殊字符
- Json文档中有多个属性,其中一个属性包含一个数组的解析
- 数组排序-包含json数据格式的数组
- json数组解析(键名[0])
- java--json嵌套数组解析(2)
- Flex解析Json数组
- 解析json数组
- json数组解析
- JSON数组解析
- JSON解析数组
- android解析json数组
- android解析JSON数组
- java解析json数组
- Json解析数组实例
- CodeForces round199Div2 B
- Android sdk manager 显示 “Done loading packages”,该怎么办?
- DDL操作产生UNDO和REDO的示例
- Unix/Linux C++应用开发-C++表达式
- Codeforces Round #199 (Div. 2)
- JSON解析数组(包含其他)
- 适定、超定和欠定方程的概念
- 字母重排
- UESTC 1832 - Never Wait for Weights 并查集 (2012 Tokyo)
- Ubuntu下apache2无法解析php文件,浏览器提示下载所要打开的php文件
- Android之监听短信
- 使用占位符对数据库进行操作
- Linux知识:/root/.bashrc与/etc/profile的异同
- 手势识别GestureOverlayView