我想解析下面給出的json。解析JSON文件
{
"sections": [
{
"title": "Title android",
"level": 1,
"content": [
{
"type": "paragraph",
"text": "This is paragraph 1 for android."
}
{
"type": "paragraph",
"text": "This is paragraph 2 for android"
}
],
"images": [
{
"src": "http://image1 android.",
"caption": "Image 1."
},
{
"src": "http://image2 android",
"caption": "Image 2."
}
]
},
{
"title": "Title java",
"level": 2,
"content": [
{
"type": "paragraph",
"text": "This is paragraph 1 for Java."
},
{
"type": "paragraph",
"text": "This is paragraph 2 for Java"
}
],
"images": [
{
"src": "http://image1 java.",
"caption": "Image 1."
},
{
"src": "http://image2 java",
"caption": "Image 2."
}
]
},
{
"title": "Title json",
"level": 3,
"content": [
{
"type": "paragraph",
"text": "This is paragraph 1 for Json."
},
{
"type": "paragraph",
"text": "This is paragraph 2 for Json"
},
{
"type": "paragraph",
"text": "This is paragraph 3 for Json"
}
],
"images": [
{
"src": "http://image1 Json.",
"caption": "Image 1."
},
{
"src": "http://image2 Json",
"caption": "Image 2."
}
]
}
我想輸出這些JSON作爲
Title 1 :Title android. \n
Content 1:This is paragraph 1 for android.
This is paragraph 2 for android.
Image 1:http:// image1 android.
Image 2:http:// image2 android.
Title :Title Java.
Content:This is paragraph 1 for Java.
This is paragraph 2 for Java.
Image 1:http:// image1 Java.
Image 2:http:// image2 Java.
...等等。
我做了什麼至今
public class ParseJSON {
public static String[] titles;
public static String[] contents;
public static String[] levels;
public static final String JSON_ARRAY = "sections";
public static final String TITLE = "title";
public static final String CONTENT = "content";
public static final String TEXT = "text";
private JSONArray sections = null;
private JSONArray content = null;
private String json;
public ParseJSON(String json) {
this.json = json;
}
protected void parseJSON() {
JSONObject jsonObject ;
try {
jsonObject = new JSONObject(json);
sections = jsonObject.getJSONArray(JSON_ARRAY);
titles = new String[sections.length()];
levels = new String[sections.length()];
for (int i = 0; i < sections.length(); i++) {
titles[i] = sections.getJSONObject(i).getString(TITLE);
JSONArray content = sections.getJSONObject(i).getJSONArray(CONTENT);
contents = new String[content.length()];
Log.d("MainActivity",contents.toString());
for (int j = 0; j < content.length(); j++) {
contents[j] += content.getJSONObject(j).getString(TEXT).toString() + "\n\n";
//Log.d("MainActivity",contents.toString());
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
上面的代碼是不完整的。 我想打印如上所述的json。 但我沒有得到所需的標題部分和段落部分。
當我解析從內容陣列TEXT它給所有段落從JSON組合作爲內容[0],內容[1]等。 但我想對於標題內容僅
我認爲環部起到一定的作用,但我不知道怎麼辦。
UPDATE 如果我想輸出作爲中間的一個alone.ie,
//android title part //not needed
//The part needed is below one:
Title :Title Java.
Content:This is paragraph 1 for Java.
This is paragraph 2 for Java.
Image 1:http:// image1 Java.
Image 2:http:// image2 Java.
//json title part //not needed
強烈考慮讓Java類將標題,內容和圖像放在一個對象下,而不是單獨的數組。改爲使用對象類型的數組。 –
我很驚訝沒有人提到過Gson? – Eenvincible
@Eenvincible你能用一個簡單的例子來幫助我解決同一個問題。謝謝 –