編輯:關於Android編程
[
{
"id": 912345678901,
"text": "How do I read JSON on Android?",
"geo": null,
"user": {
"name": "android_newb",
"followers_count": 41
}
},
{
"id": 912345678902,
"text": "@android_newb just useandroid.util.JsonReader!",
"geo": [50.454722, -104.606667],
"user": {
"name": "jesse",
"followers_count": 2
}
}
]
在代碼中,如果直接定義json數據,需要在代碼中對“ 使用 \ 轉義。上面json在代碼中的形式為:(在java代碼中,創建一段json數據,“ 符號需要轉義)
private String jsonDate = "["
+ "{\"id\": 912345678901,"
+ "\"text\":\"How do I read JSON onAndroid?\","
+ "\"geo\":null,"
+"\"user\":{\"name\":\"android_newb\",\"followers_count\":41}},"
+ "{\"id\": 912345678902,"
+ "\"text\":\"@android_newb just useandroid.util.JsonReader!\","
+ "\"geo\":[50.454722,-104.606667],"
+"\"user\":{\"name\":\"jesse\",\"followers_count\":2}}"
+ "]";
1. 使用JsonReader方法解析Json數據對象,你需要創建一個JsonReader對象. 2.然後使用beginArray()來開始解析 [ 左邊的第一個數組。 3.再使用beginObject()來開始解析數組{中的第一個對象。 4.對於直接的數據可以直接得到解析到的數據,但對於在json中嵌套了數組的數據,需要在寫一個解析方法。 5.在解析完成後,別忘用endArray(),endObject()來關閉解析。
package com.mecury.jsontest;
import java.io.IOException;
import java.io.StringReader;
import android.util.JsonReader;
import android.util.JsonToken;
public class JsonUtils {
public void parseJson(String jsonDate) throws IOException{
//創建JsonReader對象
JsonReader jsReader = new JsonReader(new StringReader(jsonDate));
jsReader.beginArray();
while (jsReader.hasNext()) {
readMessage(jsReader);
}
jsReader.endArray();
}
public void readMessage(JsonReader jsReader) throws IOException{
jsReader.beginObject();
while(jsReader.hasNext()){
String tagName =jsReader.nextName();
if(tagName.equals("id")) {
System.out.println("name:"+jsReader.nextLong());
}else if(tagName.equals("text")) {
System.out.println("text:"+jsReader.nextString());
}else if(tagName.equals("geo") && jsReader.peek()!=JsonToken.NULL) {
readDoubleArray(jsReader);
}else if(tagName.equals("user")) {
readUser(jsReader);
}else {
//跳過當前值
jsReader.skipValue();
System.out.println("skip======>");
}
}
jsReader.endObject();
}
//解析geo中的數據
public void readDoubleArray(JsonReader jsReader) throws IOException{
jsReader.beginArray();
while(jsReader.hasNext()){
System.out.println(jsReader.nextDouble());
}
jsReader.endArray();
}
//由於讀取user中的數據
public void readUser(JsonReader jsReader) throws IOException{
String userName = null;
int followsCount = -1;
jsReader.beginObject();
while (jsReader.hasNext()) {
String tagName = jsReader.nextName();
if (tagName.equals("name")) {
userName =jsReader.nextString();
System.out.println("user_name:"+ userName);
}else if (tagName.equals("followers_count")) {
followsCount = jsReader.nextInt();
System.out.println("followers_count:"+followsCount);
}
}
jsReader.endObject();
}
}
對上面的內容解析的輸出:
11-22 06:59:52.441: I/System.out(5329):name:912345678901 11-22 06:59:52.441: I/System.out(5329):text:How do I read JSON on Android? 11-22 06:59:52.461: I/System.out(5329):skip======> 11-22 06:59:52.461: I/System.out(5329):user_name:android_newb 11-22 06:59:52.471: I/System.out(5329):followers_count:41 11-22 06:59:52.481: I/System.out(5329):name:912345678902 11-22 06:59:52.491: I/System.out(5329):text:@android_newb just use android.util.JsonReader! 11-22 06:59:52.500: I/System.out(5329):50.454722 11-22 06:59:52.500: I/System.out(5329):-104.606667 11-22 06:59:52.510: I/System.out(5329):user_name:jesse 11-22 06:59:52.510: I/System.out(5329):followers_count:2
Android通過手勢實現答題器翻頁效果
本文實例為大家分享了Android答題器翻頁功能,主要使用ViewFilpper和GestureDetector來實現,供大家參考,具體內容如下1.效果圖2.實現思路把A
android設計模式之--觀察者模式
最近比較忙,好久沒有更新博客了,今天就將最近研究的一個東西總結一下,最近在維護公司的一個項目,我負責的模塊主要是下載雜志後及時的更新UI,將最新的下載進度更新到UI上,中
Android4.4 Camera 數據流分析
開門見山:這裡給出rk 在cameraHAL層的camera數據結構:typedef struct FramInfo{ int phy_addr; int v
Android ToolBar使用
Android Toolbar:ToolBar是Android 5.0(API Level 21)之後用來取代ActionBar的ToolBar的優勢:Toolbar本身