我对json很新,我在下面做了一个例子, 虽然我遇到问题,但也试图以一种好的方式解析它
我正在使用Eclipse:
JRE系统库[Java1.8]
并使用库:
的 JSON-简单1.1.1.jar
我制作了以下JSON文件:
Available kernels:
ir C:\ProgramData\Anaconda3\share\jupyter\kernels\ir
python3 C:\ProgramData\Anaconda3\share\jupyter\kernels\python3
这是我的脚本尝试读取文件
[
[
{
"TestScenario_1": {
"Transaction": "A",
"description": "This is a test A",
"Co-no": "",
"Project": "Proj1"
}
}
],
[
{
"TestScenario_2": {
"Transaction": "B",
"description": "This is a test B",
"Co-no": "",
"Project": "Proj2"
}
}
]
]
虽然我收到以下错误:
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class JsonParseTest {
private static final String filePath = "C:\\temp\\mytest2.json";
@SuppressWarnings({ "unchecked", "unused", "rawtypes" })
public static void main(String[] args) {
try {
// read the json file
FileReader reader = new FileReader(filePath);
JSONParser jsonParser = new JSONParser();
JSONObject jo = (JSONObject) jsonParser.parse(reader);
// get a String from the JSON object
Long TestScenario=(Long) jo.get("TestScenario");
System.out.println("TestScenario " + TestScenario);
String Transaction=(String) jo.get("Transaction");
System.out.println("Transaction " + Transaction);
String Description=(String) jo.get("Description");
System.out.println("Description " + Description);
String Cono=(String) jo.get("Co-no");
System.out.println("Co-no " + Cono);
JSONArray Parameters= (JSONArray) jo.get("Parameters");
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} catch (ParseException ex) {
ex.printStackTrace();
} catch (NullPointerException ex) {
ex.printStackTrace();
}
}
}
有人可以帮助我吗?
提前谢谢
答案 0 :(得分:1)
一个简单的方法可能是json字符串的递归方法,json数组和java中带有instanceof方法的对象。
所以,需要两个方法,一个用于json数组
private static void searchJSONArray(JSONArray arry) {
JSONArray jarry = arry;
JSONObject jobj = null;
for (Iterator iter = jarry.iterator(); iter.hasNext();) {
Object o = iter.next();
if (o instanceof JSONArray) {
searchJSONArray((JSONArray) o);
} else if (o instanceof JSONObject) {
jobj = (JSONObject) o;
Set keyset = jobj.keySet();
for (Iterator iter2 = keyset.iterator(); iter2.hasNext();) {
String key = (String) iter2.next();
Object value = jobj.get(key);
if(value instanceof JSONObject)
{
System.out.println("[" + key + "]");
searchJSONObject((JSONObject)value);
}
else
{
System.out.println(key + "=" + (String)value);
}
}
}
}
}
和json对象的另一个。
private static void searchJSONObject(JSONObject obj) {
JSONObject jobj = obj;
Set keyset = jobj.keySet();
for (Iterator iter2 = keyset.iterator(); iter2.hasNext();) {
String key = (String) iter2.next();
Object value = jobj.get(key);
if(value instanceof JSONObject)
{
searchJSONObject((JSONObject)value);
}
else
{
System.out.println(key + "=" + (String)value);
}
}
}
然后,完整的来源如下;
package com.tobee.tests.parse.json;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.Set;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class JsonParseTest {
private static final String filePath = "resource/aa/mytest2.json";
private static void searchJSONObject(JSONObject obj) {
JSONObject jobj = obj;
Set keyset = jobj.keySet();
for (Iterator iter2 = keyset.iterator(); iter2.hasNext();) {
String key = (String) iter2.next();
Object value = jobj.get(key);
if(value instanceof JSONObject)
{
searchJSONObject((JSONObject)value);
}
else
{
System.out.println(key + "=" + (String)value);
}
}
}
private static void searchJSONArray(JSONArray arry) {
JSONArray jarry = arry;
JSONObject jobj = null;
for (Iterator iter = jarry.iterator(); iter.hasNext();) {
Object o = iter.next();
if (o instanceof JSONArray) {
searchJSONArray((JSONArray) o);
} else if (o instanceof JSONObject) {
jobj = (JSONObject) o;
Set keyset = jobj.keySet();
for (Iterator iter2 = keyset.iterator(); iter2.hasNext();) {
String key = (String) iter2.next();
Object value = jobj.get(key);
if(value instanceof JSONObject)
{
System.out.println("[" + key + "]");
searchJSONObject((JSONObject)value);
}
else
{
System.out.println(key + "=" + (String)value);
}
}
}
}
}
public static void main(String[] args) {
try {
// read the json file
FileReader reader = new FileReader(filePath);
JSONParser jsonParser = new JSONParser();
Object jsonThing = jsonParser.parse(reader);
if (jsonThing instanceof JSONArray) {
searchJSONArray((JSONArray) jsonThing);
} else if (jsonThing instanceof JSONObject) {
JSONObject jobj = (JSONObject) jsonThing;
Set keyset = jobj.keySet();
for (Iterator iter2 = keyset.iterator(); iter2.hasNext();) {
String key = (String) iter2.next();
String value = (String) jobj.get(key);
System.out.println(key + "=" + value);
}
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} catch (ParseException ex) {
ex.printStackTrace();
} catch (NullPointerException ex) {
ex.printStackTrace();
}
}
@SuppressWarnings({ "unchecked", "unused", "rawtypes" })
public static void your_main(String[] args) {
try {
// read the json file
FileReader reader = new FileReader(filePath);
JSONParser jsonParser = new JSONParser();
JSONObject jo = (JSONObject) jsonParser.parse(reader);
// get a String from the JSON object
Long TestScenario = (Long) jo.get("TestScenario");
System.out.println("TestScenario " + TestScenario);
String Transaction = (String) jo.get("Transaction");
System.out.println("Transaction " + Transaction);
String Description = (String) jo.get("Description");
System.out.println("Description " + Description);
String Cono = (String) jo.get("Co-no");
System.out.println("Co-no " + Cono);
JSONArray Parameters = (JSONArray) jo.get("Parameters");
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} catch (ParseException ex) {
ex.printStackTrace();
} catch (NullPointerException ex) {
ex.printStackTrace();
}
}
}
输出在这里
[TestScenario_1]
Co-no=
Project=Proj1
Transaction=A
description=This is a test A
[TestScenario_2]
Co-no=
Project=Proj2
Transaction=B
description=This is a test B
答案 1 :(得分:-1)
[
{
"TestScenario_1": {
"Transaction": "A",
"description": "This is a test A",
"Co-no": "",
"Project": "Proj1"
}
},
{
"TestScenario_2": {
"Transaction": "B",
"description": "This is a test B",
"Co-no": "",
"Project": "Proj2"
}
}
]
然后
FileReader reader = new FileReader(filePath);
JSONParser jsonParser = new JSONParser();
JSONArray jo = (JSONArray) jsonParser.parse(reader);
// get a String from the JSON object
JSONObject testScenario=(JSONObject) ((JSONObject)jo.get(0)).get("TestScenario_1");
Long transaction = (Long)testScenario.get("Transaction");
System.out.println("transaction " + transaction);
`
使用for循环迭代JSONArray ......我只是举个例子