JSON转换一个数组列表,里面一个ArrayList(JAVA) [英] Convert JSON inside an array list to an ArrayList (Java)
本文介绍了JSON转换一个数组列表,里面一个ArrayList(JAVA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的输出是这样的:
{
"IssueField1":{
"id":"customfield_10561",
"name":"Bug Disclaimer",
"type":null,
"value":"<div style>...</div>"
},
"IssueField2":{
"id":"customfield_13850",
"name":"ENV Work Type (DT)",
"type":null,
"value":null
},
.
.
.
"IssueField9":{
"id":"timespent",
"name":"Time Spent",
"type":"null",
"value":"null"
}
}
我想创建一个ArrayList,并添加所有的名字在里面,如果值不为空。任何想法如何,我应该这样做在Java中?
I want to create an ArrayList and and add all names in it if the value is not null. Any idea how should I do this in Java ?
推荐答案
假设你有以下的JSON对象:
Lets say you have the following json object:
{
名:李四,
时代:100,
工作:科学家,
地址:[地址1,地址2,地址3]
}
来获取对象内的不同领域,创建一个JSONParser对象,并使用的get()
方法来获得在该领域保持的数值
to get the different fields inside of the object, create a JSONParser object and use the get()
method to get the value held in that field
try {
FileReader reader = new FileReader("/path/to/file.json");
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
String name = (String) jsonObject.get("name");
System.out.println("The name is " + name);
long age = (long) jsonObject.get("age");
System.out.println("The age is: " + age);
JSONArray lang = (JSONArray) jsonObject.get("addresses");
for (int i = 0; i < lang.size(); i++) {
System.out.println("Address " + (i + 1) + ": " + lang.get(i));
}
} catch (FileNotFoundException fileNotFound) {
fileNotFound.printStackTrace();
} catch (IOException io) {
io.printStackTrace();
} catch (NullPointerException npe) {
npe.printStackTrace();
} catch (org.json.simple.parser.ParseException e) {
e.printStackTrace();
}
}
这篇关于JSON转换一个数组列表,里面一个ArrayList(JAVA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文