Android JSON解析器教程含代碼

Android JSON解析器教程

JSON (JavaScript對象表示)是一種編程語言。愛掏網 - it200.com它是最小的、文本的,是JavaScript的一個子集。愛掏網 - it200.com它是XML的一個替代。愛掏網 - it200.com

Android提供支持來解析JSON對象和數組。愛掏網 - it200.com

JSON相對于XML的優勢

1)對于AJAX應用程序,JSON比XML更快更容易。愛掏網 - it200.com

2)與XML不同,JSON更短更快速地讀寫。愛掏網 - it200.com

3)它使用數組。愛掏網 - it200.com

JSON對象

JSON對象包含類似映射的鍵/值對。愛掏網 - it200.com鍵是字符串,值是JSON類型。愛掏網 - it200.com 鍵和值用逗號分隔。愛掏網 - it200.com{(大括號)表示JSON對象。愛掏網 - it200.com

{
    "employee": {
        "name":       "sachin", 
        "salary":      56000, 
        "married":    true
    }
}

JSON數組

[(方括號)代表JSON數組。愛掏網 - it200.com

["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

讓我們再舉一個JSON數組的例子。愛掏網 - it200.com

{ "Employee" :
    [
     {"id":"101","name":"Sonoo Jaiswal","salary":"50000"},
     {"id":"102","name":"Vimal Jaiswal","salary":"60000"}
    ] 
}

Android JSON解析的例子

activity_main.xml

從拖放窗口中拖一個textview。愛掏網 - it200.com現在activity_main.xml文件將變成這樣:

<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="75dp"
        android:layout_marginTop="46dp"
        android:text="TextView" />

</RelativeLayout>

Activity類

讓我們寫代碼使用dom解析器來解析xml。愛掏網 - it200.com

package com.javatpoint.jsonparsing;

import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {
public static final String JSON_STRING="{\"employee\":{\"name\":\"Sachin\",\"salary\":56000}}";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView textView1=(TextView)findViewById(R.id.textView1);

try{
JSONObject emp=(new JSONObject(JSON_STRING)).getJSONObject("employee");
String empname=emp.getString("name");
int empsalary=emp.getInt("salary");

String str="Employee Name:"+empname+"\n"+"Employee Salary:"+empsalary;
textView1.setText(str);

}catch (Exception e) {e.printStackTrace();}

}

}

輸出:

在Android中解析JSONArray

通過JSONArray類的幫助,您可以解析包含JSON對象的JSONArray。愛掏網 - it200.com讓我們看一個簡單的例子來解析json數組。愛掏網 - it200.com

package com.example.jsonparsing2;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {
   @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView output = (TextView) findViewById(R.id.textView1);

        String strJson="{ \"Employee\" :[{\"id\":\"101\",\"name\":\"Sonoo Jaiswal\",\"salary\":\"50000\"},{\"id\":\"102\",\"name\":\"Vimal Jaiswal\",\"salary\":\"60000\"}] }";

               String data = "";
               try {
                     // Create the root JSONObject from the JSON string.
                   JSONObject  jsonRootObject = new JSONObject(strJson);

                   //Get the instance of JSONArray that contains JSONObjects
                    JSONArray jsonArray = jsonRootObject.optJSONArray("Employee");

                    //Iterate the jsonArray and print the info of JSONObjects
                    for(int i=0; i < jsonArray.length(); i++){
                        JSONObject jsonObject = jsonArray.getJSONObject(i);

                        int id = Integer.parseInt(jsonObject.optString("id").toString());
                        String name = jsonObject.optString("name").toString();
                        float salary = Float.parseFloat(jsonObject.optString("salary").toString());

                        data += "Node"+i+" : \n id= "+ id +" \n Name= "+ name +" \n Salary= "+ salary +" \n ";
                      }
                    output.setText(data);
                 } catch (JSONException e) {e.printStackTrace();}
      }
}

下載此示例

輸出:

聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。
發表評論
更多 網友評論0 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 亚洲国产美女精品久久久久| 国产精品亚洲专区无码不卡| 另类视频区第一页| 久久久精品一区| 青青热久久久久综合精品| 有色视频在线观看免费高清在线直播| 国产精品无码专区在线观看| 亚洲欧美日韩一区在线观看| 91综合久久婷婷久久| 欧美色欧美亚洲另类二区| 国内最真实的XXXX人伦| 亚洲精品无码专区在线播放| 97在线视频精品| 欧美女人毛茸茸| 国产成人高清亚洲一区久久| 久久老色鬼天天综合网观看| 韩国一级毛片完整高清| 日本zzzzwww大片免费| 和桃子视频入口网址在线观看| 丁香六月激情综合| 福利片免费一区二区三区| 夜夜爽免费视频| 亚洲国产成人高清在线观看| 日本网址在线观看| 日本免费的一级v一片| 啊灬啊灬啊灬快灬性| xx视频在线永久免费观看| 波多野结衣一二区| 国产精品乱子乱xxxx| 久久成人国产精品| 美女张开腿让男人桶的视频| 女人脱裤子让男生桶的免费视频| 亚洲精品高清国产一久久| 在线观看你懂得| 波多野结衣69| 日韩小视频网站| 国产精品白浆在线观看无码专区| 亚洲一级毛片中文字幕| 超碰aⅴ人人做人人爽欧美| 影音先锋亚洲资源| 亚洲熟妇av一区二区三区宅男|