Java:解析JSON文件
我有一个类似这样的Json文件Java:解析JSON文件,java,string,json,Java,String,Json,我有一个类似这样的Json文件 {"Name":"Saaa","AppIcon":["ddd.jpg","Wallpaper.jpg","ddd.jpg"]} 我需要提取AppIcon值。我正在使用jsonsimplelib解析json。要分析这些值的代码段是: FileReader appIconReader = new FileReader("jsonpath.json"); JSONObject jsonIconObject = (JSONObject)jsonParser.parse(
{"Name":"Saaa","AppIcon":["ddd.jpg","Wallpaper.jpg","ddd.jpg"]}
我需要提取AppIcon
值。我正在使用jsonsimple
lib解析json
。要分析这些值的代码段是:
FileReader appIconReader = new FileReader("jsonpath.json");
JSONObject jsonIconObject = (JSONObject)jsonParser.parse(appIconReader);
System.out.println("APPLICATION ICON = "+jsonIconObject.get("AppIcon"));`
但是我得到的输出是一个字符串:
["ddd.jpg","Wallpaper.jpg","ddd.jpg"]
我需要像这样提取单个值
ddd.jpg
Wallpaper.jpg
ddd.jpg
不是用方括号([])和双引号(“”),正如我现在得到的。我该怎么做呢?的文档不是很完整,但是如果您想处理数组,需要强制转换返回的对象。get()
:
JSONArray
似乎是可以迭代的通用容器,例如:
Iterator<String> it = appIcon.iterator();
while (it.hasNext()) System.out.println(it.next());
Iterator it=appIcon.Iterator();
while(it.hasNext())System.out.println(it.next());
的文档不是很完整,但是如果要处理数组,需要强制转换返回的对象。get()
:
JSONArray
似乎是可以迭代的通用容器,例如:
Iterator<String> it = appIcon.iterator();
while (it.hasNext()) System.out.println(it.next());
Iterator it=appIcon.Iterator();
while(it.hasNext())System.out.println(it.next());
的文档不是很完整,但是如果要处理数组,需要强制转换返回的对象。get()
:
JSONArray
似乎是可以迭代的通用容器,例如:
Iterator<String> it = appIcon.iterator();
while (it.hasNext()) System.out.println(it.next());
Iterator it=appIcon.Iterator();
while(it.hasNext())System.out.println(it.next());
的文档不是很完整,但是如果要处理数组,需要强制转换返回的对象。get()
:
JSONArray
似乎是可以迭代的通用容器,例如:
Iterator<String> it = appIcon.iterator();
while (it.hasNext()) System.out.println(it.next());
Iterator it=appIcon.Iterator();
while(it.hasNext())System.out.println(it.next());
您也可以试试这个,它是列表类型中的存储值:
try {
FileReader appIconReader = new FileReader("jsonpath.json");
JSONObject jsonIconObject = (JSONObject)jsonParser.parse(appIconReader);
JSONArray lan = jsonIconObject.get("AppIcon"));
List<String> list = new ArrayList<String>();
for(int i = 0; i < arr.length(); i++){
list.add(lan.getJSONObject(i).getString("name"));
}
} catch (org.json.simple.parser.ParseException e) {
e.printStackTrace();
}
试试看{
FileReader appIconReader=newFileReader(“jsonpath.json”);
JSONObject jsonIconObject=(JSONObject)jsonParser.parse(appIconReader);
JSONArray lan=jsonIconObject.get(“AppIcon”);
列表=新的ArrayList();
对于(int i=0;i
您也可以试试这个,它是列表类型中的存储值:
try {
FileReader appIconReader = new FileReader("jsonpath.json");
JSONObject jsonIconObject = (JSONObject)jsonParser.parse(appIconReader);
JSONArray lan = jsonIconObject.get("AppIcon"));
List<String> list = new ArrayList<String>();
for(int i = 0; i < arr.length(); i++){
list.add(lan.getJSONObject(i).getString("name"));
}
} catch (org.json.simple.parser.ParseException e) {
e.printStackTrace();
}
试试看{
FileReader appIconReader=newFileReader(“jsonpath.json”);
JSONObject jsonIconObject=(JSONObject)jsonParser.parse(appIconReader);
JSONArray lan=jsonIconObject.get(“AppIcon”);
列表=新的ArrayList();
对于(int i=0;i
您也可以试试这个,它是列表类型中的存储值:
try {
FileReader appIconReader = new FileReader("jsonpath.json");
JSONObject jsonIconObject = (JSONObject)jsonParser.parse(appIconReader);
JSONArray lan = jsonIconObject.get("AppIcon"));
List<String> list = new ArrayList<String>();
for(int i = 0; i < arr.length(); i++){
list.add(lan.getJSONObject(i).getString("name"));
}
} catch (org.json.simple.parser.ParseException e) {
e.printStackTrace();
}
试试看{
FileReader appIconReader=newFileReader(“jsonpath.json”);
JSONObject jsonIconObject=(JSONObject)jsonParser.parse(appIconReader);
JSONArray lan=jsonIconObject.get(“AppIcon”);
列表=新的ArrayList();
对于(int i=0;i
您也可以试试这个,它是列表类型中的存储值:
try {
FileReader appIconReader = new FileReader("jsonpath.json");
JSONObject jsonIconObject = (JSONObject)jsonParser.parse(appIconReader);
JSONArray lan = jsonIconObject.get("AppIcon"));
List<String> list = new ArrayList<String>();
for(int i = 0; i < arr.length(); i++){
list.add(lan.getJSONObject(i).getString("name"));
}
} catch (org.json.simple.parser.ParseException e) {
e.printStackTrace();
}
试试看{
FileReader appIconReader=newFileReader(“jsonpath.json”);
JSONObject jsonIconObject=(JSONObject)jsonParser.parse(appIconReader);
JSONArray lan=jsonIconObject.get(“AppIcon”);
列表=新的ArrayList();
对于(int i=0;i
您在此处得到的答案可能重复。所以你在两个不同的网站上发布了相同的问题。这是无效的。请用您正在处理的确切JSON更新您的问题。@HotLicks:这是一个打字错误……我编辑了我的问题。这里可能有您的重复答案。所以你在两个不同的网站上发布了相同的问题。这是无效的。请用您正在处理的确切JSON更新您的问题。@HotLicks:这是一个打字错误……我编辑了我的问题。这里可能有您的重复答案。所以你在两个不同的网站上发布了相同的问题。这是无效的。请用您正在处理的确切JSON更新您的问题。@HotLicks:这是一个打字错误……我编辑了我的问题。这里可能有您的重复答案。所以你在两个不同的网站上发布了相同的问题。这是无效的。请用您正在处理的确切JSON更新您的问题。@HotLicks:那是一个打字错误……我编辑了我的问题。