嗨我正在一個動態的Android應用程序,我有以下的JSON。閱讀每一行是好的我已經想出瞭如何在java中做到這一點(請記住,我剛剛開始使用json昨天)我如何可以讀取內部數組的示例item2到Java中的ArrayList我需要某種類型的循環添加到字符串數組的屏幕截圖鏈接。感謝您對我的問題提供任何幫助。JSon閱讀內部陣列
"Manager":
[
{
"name" : "Sample item",
"icon" : "http://www.test.com/icon.png",
"link" : "http://www.test.com/sample.zip",
"summary" : "sample summary here",
"screenshots": [
"http://test.com/link2screenshot1.png",
"http://test.com/link2screenshot2.png"
]
},
{
"name" : "Sample item2",
"icon" : "http://www.test.com/icon2.png",
"link" : "http://www.test.com/sample2.zip",
"summary" : "sample summary here2",
"screenshots": [
"http://test.com/2/link2screenshot1.png",
"http://test.com/2/link2screenshot2.png"
]
}
]
===編輯===
好吧,這裏是最終的解決方案,下面的方法從URL中讀取一個JSON和拉什麼,我想感謝ninetwozero
public ArrayList<String> JSONInner(int count, String url, String outer, String inner){
ArrayList<String> linkArrayList = new ArrayList<String>();
URL u = null;
StringBuilder FileData = null;
try {
u = new URL(url);
String InputData = null;
InputStream is = null;
DataInputStream dis = null;
FileData = new StringBuilder();
is = u.openStream();
dis = new DataInputStream(new BufferedInputStream(is));
while ((InputData = dis.readLine()) != null) {
FileData.append(InputData);
}
is.close();
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
JSONObject json = null;
try {
json = new JSONObject(FileData.toString());
} catch (JSONException e1) {
e1.printStackTrace();
}
if(FileData.toString() != null) {
try {
JSONArray managerArray = json.getJSONArray(outer);
JSONObject managerObject = managerArray.optJSONObject(count);
JSONArray linkArray = managerObject.getJSONArray(inner);
for(int subCount = 0, maxSubCount = linkArray.length(); subCount < maxSubCount; subCount++){
linkArrayList.add(linkArray.getString(subCount));
}
}catch (JSONException e){
e.printStackTrace();
}
}
return linkArrayList;
}
是你是用手做還是用Jackson之類的東西? http://wiki.fasterxml.com/JacksonInFiveMinutes – redben 2012-02-04 16:38:15
@redben做手工目前我可以發佈我使用的方法來獲得我需要的線路,但如果需要的話,這很麻煩 – user577732 2012-02-04 16:41:02