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%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。