现在的位置: 首页 > 综合 > 正文

JSON解析数组

2019年07月20日 ⁄ 综合 ⁄ 共 2794字 ⁄ 字号 评论关闭

 

 

在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.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 = "[{\"id\":1,\"name\":\"李叶文\",\"age\":30},"

         + "{\"id\":2,\"name\":\"HCXY\",\"age\":10}]";

     StringBuffer buf = new StringBuffer() ;

     try {

       List<Map<String,Object>> all = this.parseJson(str) ;

       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 List<Map<String, Object>> parseJson(String data) throws Exception {

     List<Map<String, Object>> all = new ArrayList<Map<String, Object>>();

     JSONArray jsonArr = new JSONArray(data); // 是数组

     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);

     }

     return all;

  }

}

 

抱歉!评论已关闭.