我想解析下面给出的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你能用一个简单的例子来帮助我解决同一个问题。谢谢 –