简体   繁体   English

JSON解析嵌套数组对象

[英]JSON Parsing Nested Array Objects

Using Simple-JSON on the following JSON formatted file, I'm having a lot of trouble understanding how to access the objects within the array under "name". 在以下JSON格式的文件上使用Simple-JSON时,我很难理解如何访问“名称”下数组中的对象。

JSON File: JSON档案:

[
    {
        "name":{
            "firstName": "Developer",
            "lastName": "D"
        },
        "id": 00, 
        "permissionLevel": 3, 
        "password": 12345
    },
    {
        "name":{
            "firstName": "Bob",
            "lastName": "Smith"
        }, 
        "id": 01, 
        "permissionLevel": 2, 
        "password": 23456
    }
]

I'm able to obtain the information for all of the other contents because they're not located in a nested array; 我能够获取所有其他内容的信息,因为它们不在嵌套数组中; However, when I attempt to retrieve the objects under "name", all that is output is the String found in the JSON file. 但是,当我尝试检索“名称”下的对象时,输出的只是在JSON文件中找到的字符串。

Current code: 当前代码:

String[] searchData = {
                    "name",
                    "firstName",
                    "lastName",
                    "id",
                    "permissionLevel",
                    "password"
                };

jsonArray = (JSONArray)new JSONParser().parse(s);
for(int i = 0; i < jsonArray.size(); i++){
    JSONObject jo = (JSONObject)jsonArray.get(i);
    for(int j = 0; j < searchData.length; j++){
        System.out.println(
                searchData[j] + ": " + jo.get(searchData[j]));
    }
}

Output: 输出:

name: [{"firstName":"Developer","lastName":"D"}]
firstName: null
lastName: null
id: 0
permissionLevel: 3
password: 12345
name: [{"firstName":"Bob","lastName":"Smith"}]
firstName: null
lastName: null
id: 1
permissionLevel: 2
password: 23456

As you can see, "name" outputs a String from the JSON file, and not each individual value. 如您所见,“名称”从JSON文件输出一个字符串,而不是每个单独的值。

In the end, I need to write a universal code that can accept new "searchData" tags for each file that's input. 最后,我需要编写一个通用代码,该代码可以为输入的每个文件接受新的“ searchData”标签。

  1. Might someone be able to direct me how to obtain objects held within nested arrays? 也许有人可以指示我如何获取嵌套数组中的对象?
  2. Or perhaps I need to use a different Library? 还是我需要使用其他图书馆? If so, which one is the most efficient for Java? 如果是这样,哪一个对Java最有效? I'm not programming for Android, and I continue to find Library suggestions for Android, constantly. 我不是在为Android编程,因此我一直在不断寻找Android的图书​​馆建议。

My apologies if this post is a dupe, but no other posts are aiding me. 我很抱歉,如果这篇文章是虚假的,但没有其他文章可以帮助我。

You should get your firstname and lastname , like: 您应该获得您的firstnamelastname ,例如:

jo.get("name").get("firstname");
jo.get("name").get("lastname");

To get the objects held within nested arrays/objects, you will have to write a recursive method and flatten the structure into a map. 为了使对象保留在嵌套数组/对象中,您将必须编写一个递归方法并将结构展平为地图。 Below example shows the same: 下面的示例显示了相同的内容:

public static void main(String args[]) throws ParseException {
    Object object =  new JSONParser().parse("[    {        \"name\":{            \"firstName\": \"Developer\",            \"lastName\": \"D\"        },        \"id\": 00,         \"permissionLevel\": 3,         \"password\": 12345    },    {        \"name\":{            \"firstName\": \"Bob\",            \"lastName\": \"Smith\"        },         \"id\":01,         \"permissionLevel\": 2,         \"password\": 23456    }]");
    Map<String, Object> pairs = new HashMap<>();
    addValues(object, pairs);
    System.out.println(pairs);
}

public static void addValues(Object object,  Map<String, Object> pairs){
    if(object instanceof JSONObject){
        JSONObject jsonObject = (JSONObject) object;
        for(String key : jsonObject.keySet()){
            if(jsonObject.get(key) instanceof JSONObject || jsonObject.get(key) instanceof JSONArray){
                addValues(jsonObject.get(key), pairs);
            }else{
                pairs.put(key, jsonObject.get(key));
            }
        }
    }else if(object instanceof JSONArray){
        JSONArray jsonArray = (JSONArray)object;
        for(Object element : jsonArray){
            addValues(element, pairs);
        }
    }
}

You can tweak this method to have keys like name.firstname or name.lastname depending on requirements. 您可以根据需要调整此方法以使其具有name.firstname或name.lastname之类的键。

I understand that you want the searchData tags to be taken into consideration while parsing the JSON . 我了解您在解析JSON时要考虑searchData标记。 I would suggest using Google Gson for this case. 我建议在这种情况下使用Google Gson

You can write a POJO which return the ArrayList<User> for your JSON. 您可以编写一个POJO,为您的JSON返回ArrayList<User>

Refer this article on how use Google Gson 请参阅这篇文章, 了解如何使用Google Gson

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

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