當前位置:首頁 » 安卓系統 » android解析對象數組

android解析對象數組

發布時間: 2023-11-30 06:27:48

1. Android 該如何解析json數組裡面的數組

[]就是JSONArray,{}就是JSONObject
JSONObject json = new JSONObject("{xxx}");
json.getJsonArray("data").getJSONObject(0).getJsonArray("task_photo");

2. android怎樣解析Json並列序列的數組

[["北京市"],["上海市"],["合肥市","蕪湖市","蚌埠市"]] 這是一個JsonArray ,先獲取JsonArray對象,然後再次獲取獲取JsonArray對象:子JsonArray對象=父JsonArray對象.get(index值);最後,獲取子JsonArray對象裡面的數據,子JsonArray對象.get(index值);

3. Android 中解析 JSON

JSON( JavaScript Object Notation ) 是一種輕量級的數據交換格式。易於閱讀和編寫,同時也易於機器解析和生成。

JSON 建構於兩種結構:

JSON 具有以下這些格式:

參考: Android 中 解析 JSON

Android 提供類四種不同的類來操作 JSON 數據。這些類是 JSONArray、JSONObject、JSONStringer 和 JSONTokenizer

為了解析 JSON 對象,須先創建一個 JSONObject 類的對象,需要傳入需解析的字元串 JSONObject root = new JSONObject(candyJson); 然後根據 JSONObject 對象提供方法以及數據類型解析對應 json 數據。下表展示一些 JSONObiect 提供的方法

示例:

4. android如何從網頁中獲取一個json數組並解析出來,顯示在textview裡面

1,先要建立一個線程獲取json數據
2接著解析json數據
3,設置textview
例如:json數據,strjson= {"key": ["a","b"]}
JSONObject object = new JSONObject (strjson)
JSONArray arr= object.getJSONArray("key") ;
String text=arr.getString(0);

5. android 怎麼看json數據格式

給服務端發送請求後,服務端會返回一連串的數據,這些數據在大部分情況下都是XML格式或者JSON格式。然後JSON相對XML來說解析相對方便一些,所以先說說JSON的解析。
JSON的基本數據格式有這幾種:
1.一個JSON對象——JSONObject
{"name":"胡小威" , "age":20 , "male":true}
2.一個JSON數組——JSONArray
[{"name":"胡小威" , "age":20 , "male":true},{"name":"趙小亮" , "age":22 , "male":false}]
3.復雜一點的JSONObject
{"name":"胡小威", "age"=20, "male":true, "address":{"street":"嶽麓山南", "city":"長沙","country":"中國"}}
4.復雜一點的JSONArray
[
{"name":"胡小威", "age"=20, "male":true, "address":{"street":"嶽麓山南", "city":"長沙","country":"中國"}},
{"name":"趙小亮", "age"=22, "male":false, "address":{"street":"九州港", "city":"珠海","country":"中國"}}
]
5.來個更復雜的,這是我從愛幫公交上扒取的數據
{"buses":{"bus":[{"last_foot_dist":"0","time":"37","segments":{"segment":[{"line_name":"立珊專線(中南大學學生公寓-長沙火車站)","foot_dist":"362","stat_xys":"","stats":"嶽麓山南;湖南師大;二里半;嶽麓山北;市四醫院;華圖教育(太平街口);牛耳教育(南陽街口);韭菜園;曙光路口;長島路口;長沙火車站","end_stat":"長沙火車站","line_xys":"","line_dist":"7535","start_stat":"嶽麓山南"}]},"foot_dist":"362","dist":"7897"},{"last_foot_dist":"0","time":"41","segments":{"segment":[{"line_name":"旅1路(科教新村-長沙火車站)","foot_dist":"337","stat_xys":"","stats":"嶽麓山南;湖南師大;二里半;嶽麓山北;市四醫院;華圖教育(太平街口);蔡鍔中路口;韭菜園;喬庄;曙光路口;長島路口;五一東村;車站路口;長沙火車站","end_stat":"長沙火車站","line_xys":"","line_dist":"7822","start_stat":"嶽麓山南"}]},"foot_dist":"337","dist":"8159"}]}}

接下來是解析部分
一、直接解析
1.{"name":"胡小威" , "age":20 , "male":true}

Java代碼
public void parseJsonObject(String json) {
try {
JSONObject jsonObject = new JSONObject(json);
String name = jsonObject.getString("name");
int age = jsonObject.getInt("age");
boolean male = jsonObject.getBoolean("male");
//接下來該幹嘛幹嘛
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

2.[{"name":"胡小威" , "age":20 , "male":true},{"name":"趙小亮" , "age":22 , "male":false}]

Java代碼
public void parseJSONArray(String json) {
try {
JSONArray jsonArray = new JSONArray(json);
for (int i = 0; i < jsonArray.length(); i++) {
jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.getString("name");
int age = jsonObject.getInt("age");
boolean male = jsonObject.getBoolean("male");
//接下來該幹嘛幹嘛
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

3.{"name":"胡小威", "age"=20, "male":true, "address":{"street":"嶽麓山南", "city":"長沙","country":"中國"}}

Java代碼
public void parseJsonPerson(String json) {
try {
JSONObject jsonObject = new JSONObject(json);
String name = jsonObject.getString("name");
int age = jsonObject.getInt("age");
boolean male = jsonObject.getBoolean("male");
JSONObject addressJSON = jsonObject.getJSONObject("address");
String street = addressJSON.getString("street");
String city = addressJSON.getString("city");
String country = addressJSON.getString("country");
Address address = new Address(street, city, country);
Person person = new Person(name, age, male, address);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

4.
[
{"name":"胡小威", "age"=20, "male":true, "address":{"street":"嶽麓山南", "city":"長沙","country":"中國"}},
{"name":"趙小亮", "age"=22, "male":false, "address":{"street":"九州港", "city":"珠海","country":"中國"}}
]

Java代碼
public void parseJsonArrayPerson(String json) {
List<Person> persons = new ArrayList<Person>();
try {
jsonArray = new JSONArray(json);
for (int i = 0; i < jsonArray.length(); i++) {
jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.getString("name");
int age = jsonObject.getInt("age");
boolean male = jsonObject.getBoolean("male");
JSONObject addressJSON = jsonObject.getJSONObject("address");
String street = addressJSON.getString("street");
String city = addressJSON.getString("city");
String country = addressJSON.getString("country");
Address address = new Address(street, city, country);
Person person = new Person(name, age, male, address);
persons.add(person);
Log.v("juno", person.toString());
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.v("juno", persons.toString());
}

5. {"buses":{"bus":[{"last_foot_dist":"0","time":"37","segments":{"segment":[{"line_name":"立珊專線(中南大學學生公寓-長沙火車站)","foot_dist":"362","stat_xys":"","stats":"嶽麓山南;湖南師大;二里半;嶽麓山北;市四醫院;華圖教育(太平街口);牛耳教育(南陽街口);韭菜園;曙光路口;長島路口;長沙火車站","end_stat":"長沙火車站","line_xys":"","line_dist":"7535","start_stat":"嶽麓山南"}]},"foot_dist":"362","dist":"7897"},{"last_foot_dist":"0","time":"41","segments":{"segment":[{"line_name":"旅1路(科教新村-長沙火車站)","foot_dist":"337","stat_xys":"","stats":"嶽麓山南;湖南師大;二里半;嶽麓山北;市四醫院;華圖教育(太平街口);蔡鍔中路口;韭菜園;喬庄;曙光路口;長島路口;五一東村;車站路口;長沙火車站","end_stat":"長沙火車站","line_xys":"","line_dist":"7822","start_stat":"嶽麓山南"}]},"foot_dist":"337","dist":"8159"}]}}

Java代碼
public List<Salution> parseJSON(String str){
Log.d("返回的JSON數據:", str);
List<Salution> busList = new ArrayList<Salution>();

try {
JSONObject jsonObject = new JSONObject(str);
JSONObject busesJSON = jsonObject.getJSONObject("buses");
JSONArray busJSArray=busesJSON.getJSONArray("bus");
for(int i=0;i<busJSArray.length();i++){
JSONObject busJSObject = busJSArray.getJSONObject(i);
JSONObject segsJSObject=busJSObject.getJSONObject("segments");
JSONArray segJSArray=segsJSObject.getJSONArray("segment");
Salution st=new Salution();

for(int j=0;j<segJSArray.length();j++){
if(j==0){
JSONObject segJSObject = segJSArray.getJSONObject(j);
st.setLine_name1(segJSObject.getString("line_name"));
Log.d("line_name", segJSObject.getString("line_name"));
st.setStats1(segJSObject.getString("stats"));
st.setEnd_stat1(segJSObject.getString("end_stat"));
st.setStart_stat1(segJSObject.getString("start_stat"));
}
if(j==1){
JSONObject segJSObject = segJSArray.getJSONObject(j);
st.setLine_name2(segJSObject.getString("line_name"));
Log.d("line_name", segJSObject.getString("line_name"));
st.setStats2(segJSObject.getString("stats"));
st.setEnd_stat2(segJSObject.getString("end_stat"));
st.setStart_stat2(segJSObject.getString("start_stat"));
}
}

busList.add(st);
}
return busList;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

這里我只是簡單獲取了幾個我想要的數據。

二、使用JSON類庫Gson 來解析JSON
Gson 是 Google 提供的用來在 Java 對象和 JSON 數據之間進行映射的 Java 類庫。可以將一個 JSON 字元串轉成一個 Java 對象,或者反過來。

首先,從 code.google.com/p/google-gson/downloads/list下載GsonAPI

Java代碼
google-gson-1.7.1-release.zip

再把gson-1.7.jar 到libs
可以使用兩種方法解析

①、通過獲取JsonReader對象解析JSON數據:

Java代碼
String jsonData = "[{\"username\":\"arthinking\",\"userId\":001},{\"username\":\"Jason\",\"userId\":002}]";
try{
JsonReader reader = new JsonReader(new StringReader(jsonData));
reader.beginArray();
while(reader.hasNext()){
reader.beginObject();
while(reader.hasNext()){
String tagName = reader.nextName();
if(tagName.equals("username")){
System.out.println(reader.nextString());
}
else if(tagName.equals("userId")){
System.out.println(reader.nextString());
}
}
reader.endObject();
}
reader.endArray();
}
catch(Exception e){
e.printStackTrace();
}

②、通過把JSON數據映射成一個對象,使用Gson對象的fromJson()方法獲取一個對象數組進行操作
Java代碼
Type listType = new TypeToken<LinkedList<User>>(){}.getType();
Gson gson = new Gson();
LinkedList<User> users = gson.fromJson(jsonData, listType);
for (Iterator iterator = users.iterator(); iterator.hasNext();) {
User user = (User) iterator.next();
System.out.println(user.getUsername());
System.out.println(user.getUserId());
}

我一般是採用直接解析,添加類庫神馬的最討厭了···
還有,抱怨一下,iteye的編輯弱爆了!!!

轉載

熱點內容
可以解壓war包的編譯軟體 發布:2025-01-23 04:38:28 瀏覽:986
vivo手機有編譯功能嗎 發布:2025-01-23 04:31:57 瀏覽:568
自己架設雲手機伺服器 發布:2025-01-23 04:31:17 瀏覽:949
gcc命令行編譯的方法 發布:2025-01-23 04:30:31 瀏覽:397
我的雲伺服器地址近期價格 發布:2025-01-23 04:29:05 瀏覽:625
js預覽上傳圖片 發布:2025-01-23 04:28:54 瀏覽:407
特斯拉在哪裡輸入密碼 發布:2025-01-23 04:05:29 瀏覽:205
影視腳本創作 發布:2025-01-23 04:00:39 瀏覽:844
cmd腳本執行sql腳本 發布:2025-01-23 03:46:51 瀏覽:115
搭建100人的游戲伺服器 發布:2025-01-23 03:37:43 瀏覽:517