I am trying to parse this JSON in Java.
{
"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
}
So far i have done the following using the gson library :-
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
I need help from here, I am not able to parse this JSONObject, I cannot get a JSonArray out of it and I am not able to get anything like a primitive etc. I can see in the debugger it has all the information I need. It is unreadable because of the spaces in between.
Can you please let me know how I can parse the rest of this JSON.
Thank you very much.
1 Answer 1
I used Java API for JSON Processing. Jar can be found here.
This updated version should help with multiple JSON objects separated by commas. This version attempts to find the correct comma then splits the string based on those commas.
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月12日T12: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月12日T12: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月12日T12:03:53.716Z
_op
add
_id
2
9 Comments
String. Next, I would split the String on the comma. Finally, I would handle each split ArrayList/ Array of Strings individually.JSON and you only want to split on a specific one.
getAsJsonObject(or whatever it is called) instead?