简体   繁体   English

解析复杂的JSON文件

[英]Parsing a complex JSON file

I need to parse a JSON file and put the data into a HTML table. 我需要解析一个JSON文件并将数据放入HTML表中。 I am using GWT for this application, the data shall be read from the file on the server side and passed to the client on page load. 我正在为此应用程序使用GWT,应从服务器端的文件中读取数据,并在页面加载时将其传递给客户端。

The format for the JSONObjects in the file are as follows: 文件中JSONObjects的格式如下:

{  
"Object 1": [
{ "value1": [ "subKey1", "subValue2" ], "value2": "val", "value3": { "key1": val1, "key2": val2, "key3": val3} },
{ "value2": [ "subKey1", "subValue2" ], "value2": "val", "value3": { "key1": val1, "key2": val2, "key3": val3} },
....
....
],

"Object 2": [
{ "value1": [ "subKey1", "subValue2" ], "value2": "val", "value3": { "key1": val1, "key2": val2, "key3": val3} },
{ "value2": [ "subKey1", "subValue2" ], "value2": "val", "value3": { "key1": val1, "key2": val2, "key3": val3} },
....
....
],
 ....

}

Up until now, I have only done simple JSON parsing. 到目前为止,我只完成了简单的JSON解析。 The problem I am having here is that the data I am working with has a unique name for each object so I cannot seem to parse them into an array of JSONObjects. 我在这里遇到的问题是,我正在使用的数据对每个对象都有一个唯一的名称,因此我似乎无法将它们解析为JSONObjects数组。

I have attempted to parse them (using JSON simple) this way but I am throwing an error. 我试图以这种方式解析它们(使用JSON简单),但是我抛出一个错误。

    try {
        JSONParser parser = new JSONParser();
        JSONObject obj;
            obj = (JSONObject) parser.parse(new FileReader("file.json"));

        JSONArray array = new JSONArray();
        array.add(obj.get("Object1"));
        array.add(obj.get("Object2"));
        array.add(obj.get("Object3"));
        array.add(obj.get("Object4"));

        JSONObject jo;

        for (Object o : array) {
            jo = (JSONObject) o;
        }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } 

But this throws an error: 但这会引发错误:

    org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject

Another method from my understanding is to create a POJO class for the objects but since each JSONObject has a different identifier, does that mean each object must have its own unique class? 根据我的理解,另一种方法是为对象创建POJO类,但是由于每个JSONObject具有不同的标识符,是否意味着每个对象都必须具有自己的唯一类? Some JSON2Java methods I have used just create a new class for each of them. 我使用的一些JSON2Java方法仅为每个方法创建一个新类。

You can check the instance of the Object before casting it: 您可以在投射对象之前检查对象的实例:

for (Object obj : array) {
    if (obj instanceof JSONArray) {
       // It's an array
       yourJsonArray = (JSONArray)obj;
    } else if (obj instanceof JSONObject) {
       // It's an object
       yourJsonObject = (JSONObject)obj;
    } else {
       // It's string, number...
    }
 }

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

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