简体   繁体   English

解析复杂的JSON结构

[英]parsing a Complex JSON Structure

I am trying to parse this JSON in Java. 我正在尝试用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
}

So far i have done the following using the gson library :- 到目前为止,我已经使用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

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. 我需要从这里获得帮助,我无法解析此JSONObject,我无法从其中获取JSonArray,也无法获取类似原始资源的任何内容。在调试器中可以看到它具有我需要的所有信息。 It is unreadable because of the spaces in between. 由于之间存在空格,因此无法读取。

Can you please let me know how I can parse the rest of this JSON. 您能否让我知道如何解析此JSON的其余部分。

Thank you very much. 非常感谢你。

I used Java API for JSON Processing . 我使用Java API进行JSON处理 Jar can be found here . 罐子可以在这里找到。 This updated version should help with multiple JSON objects separated by commas. 此更新版本应有助于使用逗号分隔多个JSON对象。 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-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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM