您的位置:首页 > 移动开发 > Android开发

Android Json解析

2016-02-16 14:48 615 查看
package com.example.json;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.res.AssetManager;
import android.test.AndroidTestCase;
import android.util.Log;

public class MyTest extends AndroidTestCase {

public void testJson() {

String s = "{\"name\":\"Android���\"}";

try {
JSONObject mJsonObject = new JSONObject(s);
String bookName = mJsonObject.getString("name");
Log.d("tag", "bookName :" + bookName);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void testJsonBook() {

String s = "{\"book\":{\"id\":100,\"name\":\"Android���\"}}";

try {
JSONObject mJSONObject = new JSONObject(s);
JSONObject JSONObjcet1 = mJSONObject.getJSONObject("book");
int bookId = JSONObjcet1.getInt("id");
String bookName = JSONObjcet1.getString("name");
Log.d("tag", "bookName :" + bookName + ", bookId :" + bookId);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public void testJsonArray() {

String s = "[{\"book\":{\"id\":100,\"name\":\"Android���\"}},{\"book\":{\"id\":101,\"name\":\"Java���\"}}]";

try {
JSONArray mJSONArray = new JSONArray(s);
int length = mJSONArray.length();
for(int i=0;i<length;i++){

JSONObject mJSONObject = mJSONArray.getJSONObject(i);
JSONObject jsonBook = mJSONObject.getJSONObject("book");
int bookId = jsonBook.getInt("id");
String bookName = jsonBook.getString("name");
Log.d("testJsonArray", "bookName:"+bookName+",bookId:"+bookId);
}

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
public void testJsonObjectBooks() throws JSONException {
AssetManager mAssetManager = getContext().getAssets();
InputStream in = null;
try {
in = mAssetManager.open("json");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

String s = getStringFromInputStream(in);

JSONObject mJSONObject = null;
try {
mJSONObject = new JSONObject(s);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONArray mJSONArray = mJSONObject.getJSONArray("books");
int length = mJSONArray.length();
for(int i=0;i<length;i++){

JSONObject mJSONObject1 = mJSONArray.getJSONObject(i);
JSONObject jsonBook = mJSONObject1.getJSONObject("book");
String bookName = jsonBook.getString("name");
int bookId = jsonBook.getInt("id");

Log.e("testJsonObjectBooks", "bookName :" + bookName + ", bookId :" + bookId);

}

}
public String getStringFromInputStream(InputStream in){
BufferedReader br = new BufferedReader(new InputStreamReader(in));

//StringBuilder 线程不安全 通常用在单线程中

StringBuilder stringBuilder = new StringBuilder();
String line = "";

try {
while((line = br.readLine()) !=null ){

stringBuilder.append(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stringBuilder.toString();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: