Ich versuche, die unten angegebene JSON zu analysieren.JSON-Datei analysieren
{
"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."
}
]
}
Ich möchte diese Json als
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.
... und so weiter ausgeben.
Was ich
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();
}
}
}
Der obige Code bisher getan haben, ist nicht vollständig. Ich möchte den JSON wie oben drucken. Aber ich bekomme nicht den Titel Teil und Absatz Teil wie benötigt.
Wenn ich den TEXT von Inhalt Array analysieren gibt es alle Absätze kombiniert aus dem JSON als Inhalt [0], Inhalt [1] und so weiter. Aber ich möchte Inhalte in Bezug auf Titel nur
Ich denke, der Link-Teil spielt eine Rolle, aber ich weiß nicht wie.
UPDATE Was passiert, wenn ich die Ausgabe als mittlere alone.ie wollen,
//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
Wir empfehlen dringend, eine Java-Klasse zu erstellen, die den Titel, den Inhalt und die Bilder unter einem Objekt und nicht als separate Arrays zusammenhält. Verwenden Sie stattdessen ein Array für den Objekttyp. –
Ich bin überrascht, dass niemand Gson erwähnt hat? – Eenvincible
@Eenvincible können Sie mir mit einem kurzen Beispiel mit gson für die gleiche Frage helfen. Danke –