gpt4 book ai didi

java - 解析复杂的 JSON 结构

转载 作者:行者123 更新时间:2023-12-02 12:13:47 31 4
gpt4 key购买 nike

我正在尝试用 Java 解析此 JSON。

{
"json": {
"abc": 0,
"def": "100",
"ghi": 1,
"jkl": "0000000000",
"mno": "3",
"pqr": "COLS, COMPLETE",
"stu": 2,
"vwx": "0000010000",
"yz": "00",
"a123": 31,
"b123": 1,
"c123": "1270",
"d123": "2",
"e123": "00",
"f123": 1,
"g123": "0000001000"
},
"_indexes": {
"c123": [
"1270"
],
"h123": [
"1270"
]
},
"_d": false,
"_dd": "2017-09-12T12:03:53.716Z",
"_op": "add",
"_id": 2
}

到目前为止,我已经使用 gson 库完成了以下操作:-

         JsonParser par = new JsonParser();
JsonElement jsonelement = par.parse(new FileReader("ddJob.json"));
JsonArray jsonArr = jsonelement.getAsJsonArray();
for(Object o : jsonArr)
{
JsonObject obj = (JsonObject)(o);
JsonObject mArry = obj.getAsJsonObject();
String keyVal = mArry.get("key").toString();

if(keyVal.equalsIgnoreCase("\"jsonstore.LinkTree\""))
{
if(mArry.isJsonArray())
{
System.out.println("It is a JSONArray");
}
else if(mArry.isJsonObject())
{
System.out.println("It is a JSONObject");
}
else if(mArry.isJsonPrimitive())
{
System.out.println("It is a JSONPrimitive");
}
else if(mArry.isJsonNull())
{
System.out.println("It is a JSONNull");
}
}
// Here it prints ,it is a JSONObject
<小时/>

我需要这里的帮助,我无法解析这个 JSONObject,我无法从中获取 JsonArray,并且我无法获取类似原语等的任何内容。我可以在调试器中看到它包含所有信息我需要。由于中间有空格,因此无法读取。

您能否告诉我如何解析此 JSON 的其余部分。

非常感谢。

最佳答案

我用了Java API for JSON Processing 。 jar 可以找到here此更新版本应该有助于处理用逗号分隔的多个 JSON 对象。此版本尝试找到正确的逗号,然后根据这些逗号分割字符串。

import java.io.StringReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.json.Json;
import javax.json.stream.JsonParser;
import javax.json.stream.JsonParser.Event;
import static javax.json.stream.JsonParser.Event.KEY_NAME;
import static javax.json.stream.JsonParser.Event.VALUE_FALSE;
import static javax.json.stream.JsonParser.Event.VALUE_NUMBER;
import static javax.json.stream.JsonParser.Event.VALUE_STRING;
import static javax.json.stream.JsonParser.Event.VALUE_TRUE;

/**
*
* @author blj0011
*/
public class JSONParserTest {


/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String jsonString = "{\n" +
" \"json\": {\n" +
" \"abc\": 0,\n" +
" \"def\": \"100\",\n" +
" \"ghi\": 1,\n" +
" \"jkl\": \"0000000000\",\n" +
" \"mno\": \"3\",\n" +
" \"pqr\": \"COLS, COMPLETE\",\n" +
" \"stu\": 2,\n" +
" \"vwx\": \"0000010000\",\n" +
" \"yz\": \"00\",\n" +
" \"a123\": 31,\n" +
" \"b123\": 1,\n" +
" \"c123\": \"1270\",\n" +
" \"d123\": \"2\",\n" +
" \"e123\": \"00\",\n" +
" \"f123\": 1,\n" +
" \"g123\": \"0000001000\"\n" +
" },\n" +
" \"_indexes\": {\n" +
" \"c123\": [\n" +
" \"1270\"\n" +
" ],\n" +
" \"h123\": [\n" +
" \"1270\"\n" +
" ]\n" +
" },\n" +
" \"_d\": false,\n" +
" \"_dd\": \"2017-09-12T12:03:53.716Z\",\n" +
" \"_op\": \"add\",\n" +
" \"_id\": 2\n" +
"},\n" +
"{\n" +
" \"json\": {\n" +
" \"abc\": 0,\n" +
" \"def\": \"100\",\n" +
" \"ghi\": 1,\n" +
" \"jkl\": \"0000000000\",\n" +
" \"mno\": \"3\",\n" +
" \"pqr\": \"COLS, COMPLETE\",\n" +
" \"stu\": 2,\n" +
" \"vwx\": \"0000010000\",\n" +
" \"yz\": \"00\",\n" +
" \"a123\": 31,\n" +
" \"b123\": 1,\n" +
" \"c123\": \"1270\",\n" +
" \"d123\": \"2\",\n" +
" \"e123\": \"00\",\n" +
" \"f123\": 1,\n" +
" \"g123\": \"0000001000\"\n" +
" },\n" +
" \"_indexes\": {\n" +
" \"c123\": [\n" +
" \"1270\"\n" +
" ],\n" +
" \"h123\": [\n" +
" \"1270\"\n" +
" ]\n" +
" },\n" +
" \"_d\": false,\n" +
" \"_dd\": \"2017-09-12T12:03:53.716Z\",\n" +
" \"_op\": \"add\",\n" +
" \"_id\": 2\n" +
"},\n" +
"{\n" +
" \"json\": {\n" +
" \"abc\": 0,\n" +
" \"def\": \"100\",\n" +
" \"ghi\": 1,\n" +
" \"jkl\": \"0000000000\",\n" +
" \"mno\": \"3\",\n" +
" \"pqr\": \"COLS, COMPLETE\",\n" +
" \"stu\": 2,\n" +
" \"vwx\": \"0000010000\",\n" +
" \"yz\": \"00\",\n" +
" \"a123\": 31,\n" +
" \"b123\": 1,\n" +
" \"c123\": \"1270\",\n" +
" \"d123\": \"2\",\n" +
" \"e123\": \"00\",\n" +
" \"f123\": 1,\n" +
" \"g123\": \"0000001000\"\n" +
" },\n" +
" \"_indexes\": {\n" +
" \"c123\": [\n" +
" \"1270\"\n" +
" ],\n" +
" \"h123\": [\n" +
" \"1270\"\n" +
" ]\n" +
" },\n" +
" \"_d\": false,\n" +
" \"_dd\": \"2017-09-12T12:03:53.716Z\",\n" +
" \"_op\": \"add\",\n" +
" \"_id\": 2\n" +
"}"; //I am guessing this is how your data looks.


List<Integer> positions = new ArrayList();
Pattern p = Pattern.compile("\\},\n\\{");//Find this pattern ***MAKE SURE THIS PATTERN IS CORRECT FOR YOU*** Your patter may be "\\},\\{"
Matcher m = p.matcher(jsonString);
while(m.find())
{
positions.add(m.start() + 1);//save starting position of the found pattern
}

System.out.println("# of positions: " + positions.size());
List<String> jsonStringObjects = new ArrayList();

if(positions.size() >= 1)
{
jsonStringObjects.add(jsonString.substring(0, positions.get(0)));//get first jsonString
//System.out.println(jsonString.substring(0, positions.get(0)));
jsonStringObjects.add(jsonString.substring(positions.get(positions.size() - 1) + 1));//get last jsonString
//System.out.println(jsonString.substring(positions.get(positions.size() - 1) + 1));
}
if(positions.size() >= 2 )
for(int i = 0; i < positions.size() - 1; i++)//get all jsonStrings between first and last
{
jsonStringObjects.add(jsonString.substring(positions.get(i) + 1, positions.get(i + 1)));
//System.out.println(jsonString.substring(positions.get(i) + 1, positions.get(i + 1)));
}

System.out.println("# of jsonStringObjects: " + jsonStringObjects.size());
int counter = 0;
for(String item : jsonStringObjects)
{
System.out.println("JSON Object #: " + ++counter);
try (JsonParser parser = Json.createParser(new StringReader(item))) {
while (parser.hasNext()) {
final Event event = parser.next();
switch (event) {
case KEY_NAME:
String key = parser.getString();
System.out.println("\t" + key);
break;
case VALUE_STRING:
String value = parser.getString();
System.out.println("\t" + value);
break;
case VALUE_NUMBER:
BigDecimal number = parser.getBigDecimal();
System.out.println("\t" + number);
break;
case VALUE_TRUE:
System.out.println("\t" + true);
break;
case VALUE_FALSE:
System.out.println("\t" + false);
break;
}
}
}
}
}

}

Output:

run:
# of positions: 2
# of jsonStringObjects: 3
JSON Object #: 1
json
abc
0
def
100
ghi
1
jkl
0000000000
mno
3
pqr
COLS, COMPLETE
stu
2
vwx
0000010000
yz
00
a123
31
b123
1
c123
1270
d123
2
e123
00
f123
1
g123
0000001000
_indexes
c123
1270
h123
1270
_d
false
_dd
2017-09-12T12:03:53.716Z
_op
add
_id
2
JSON Object #: 2
json
abc
0
def
100
ghi
1
jkl
0000000000
mno
3
pqr
COLS, COMPLETE
stu
2
vwx
0000010000
yz
00
a123
31
b123
1
c123
1270
d123
2
e123
00
f123
1
g123
0000001000
_indexes
c123
1270
h123
1270
_d
false
_dd
2017-09-12T12:03:53.716Z
_op
add
_id
2
JSON Object #: 3
json
abc
0
def
100
ghi
1
jkl
0000000000
mno
3
pqr
COLS, COMPLETE
stu
2
vwx
0000010000
yz
00
a123
31
b123
1
c123
1270
d123
2
e123
00
f123
1
g123
0000001000
_indexes
c123
1270
h123
1270
_d
false
_dd
2017-09-12T12:03:53.716Z
_op
add
_id
2

关于java - 解析复杂的 JSON 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46344761/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com