java解析JSON文件
我试图解析下面给定的json
{
"sections": [
{
"title": "Title 安卓",
"level": 1,
"content": [
{
"type": "paragraph",
"text": "This is paragraph 1 for 安卓."
}
{
"type": "paragraph",
"text": "This is paragraph 2 for 安卓"
}
],
"images": [
{
"src": "http://image1 安卓.",
"caption": "Image 1."
},
{
"src": "http://image2 安卓",
"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 安卓. \n
Content 1:This is paragraph 1 for 安卓.
This is paragraph 2 for 安卓.
Image 1:http:// image1 安卓.
Image 2:http:// image2 安卓.
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。 但我没有得到标题部分和段落部分的需要
当我解析content数组中的文本时,它将json中的所有段落组合为content[0]、content[1]等等。 但我只想要标题方面的内容
我认为链接部分发挥了一些作用,但我不知道如何发挥作用
更新 如果我想把输出单独作为中间的一个呢。即
//安卓 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
# 1 楼答案
使用像this这样的json转换为pojo工具,您可以生成简单的旧Java对象(pojo),您可以使用它轻松地操作json结果
通过您的json字符串,我能够生成以下Java类:
然后,这表示图像实体:
这是包含两个对象的伞,如列表所示:
在生成它们并像任何其他类一样保存在项目中之后,现在可以使用
Gson
将json字符串转换为这些具有属性的对象由于您在json响应中获得了
List
个节,所以您可以做的很简单:现在,您有了一个可以循环获取图像和内容的部分列表。记住,内容有自己的列表,就像图像一样。因此,您应该能够轻松获取数据
我希望这对你有帮助
您可以使用Gradle添加Gson或下载jar文件并添加到android studio中的
/libs
文件夹# 2 楼答案
我已经在Logcat上打印了输出,您可以将其添加到字符串数组中,或者更好地使用所需的参数创建对象
# 3 楼答案
为什么要手动解析JSON来折磨自己?我建议您使用一些轻量级JSON解析器。这是我在Android中使用
org.codehaus.jackson
映射器实现的方式:对于您的情况,只需将JSON字符串和预期的结果类型传递给第一个方法,如
TitleWithImages.class
。 这将是Maven依赖项(但您使用的是Android studio):