简体   繁体   English

从JSON获取子数组

[英]Get sub-array from JSON

I parsing some data from a json file. 我从json文件解析一些数据。 Here is my JSON File. 这是我的JSON文件。

[
     {

       "topic": "Example1", 
       "contact": [
            {
                "ref": [
                    1
                ], 
                "corresponding": true, 
                "name": "XYZ"
            }, 
            {
                "ref": [
                    1
                ], 
                "name": "ZXY"
            }, 
            {
                "ref": [
                    1
                ], 
                "name": "ABC"
            }, 
            {
                "ref": [
                    1, 
                    2
                ], 
                "name":"BCA"
            }
        ] , 

        "type": "Presentation"
     }, 
    {

       "topic": "Example2", 
       "contact": [
            {
                "ref": [
                    1
                ], 
                "corresponding": true, 
                "name": "XYZ"
            }, 
            {
                "ref": [
                    1
                ], 
                "name": "ZXY"
            }, 
            {
                "ref": [
                    1
                ], 
                "name": "ABC"
            }, 
            {
                "ref": [
                    1, 
                    2
                ], 
                "name":"BCA"
            }
        ] , 

        "type": "Poster"
     }
]

I can fetch and store data one by one. 我可以一张一张地获取和存储数据。 Like this one 像这个

JSONArray getContactsArray = new JSONArray(jsonObject.getString("contact"));
for(int a =0 ; a < getContactsArray.length(); a++)
{
    JSONObject getJSonObj = (JSONObject)getContactsArray.get(a);
    String Name = getJSonObj.getString("name");
} 

1)Now, my question is there any way to get all name values for each array with single query. 1)现在,我的问题是有什么方法可以通过单个查询获取每个数组的所有name值。 2) Can I get all those values in an Array ? 2)我可以在Array获取所有这些值吗?

Please correct me, if I am doing anything wrong. 如果我做错任何事,请纠正我。 Thank you. 谢谢。

Iteration cannot be avoided here as org.json and other Json parsers as well provide random access to objects but not to their properties collectively (as a collection). 这里无法避免迭代,因为org.json和其他Json解析器还提供对对象的随机访问,但不能提供对对象属性的集体访问(作为集合)。 So, you can't query something like "all name properties of all contact objects" unless you probably get a Json parser like Gson to unmarshall it that way. 因此,您无法查询“所有联系人对象的全名属性”之类的信息,除非您可能会像Gson这样的Json解析器以这种方式解组。

But, that's too much to just avoid a for loop when you can definitely shorten the parse by making use of the appropriate API methods to avoid unnecessary object casts. 但是,当您可以使用适当的API方法来避免不必要的对象强制转换来绝对缩短解析时,仅避免for循环实在是太多了。

JSONArray contacts = jsonObject.getJSONArray("contact");
String[] contactNames = new String[contacts.length()];
for(int i = 0 ; i < contactNames.length; i++) {
    contactNames[i] = contacts.getJSONObject(i).getString("name");
} 

Better to use a json parser such as GSon or Jackson to marshall your json to a java object. 最好使用GSonJackson之类的json解析器将json编组为java对象。 Then you can write utitlity method in your java class to retrieve all the names in that object. 然后,您可以在Java类中编写utitlity方法来检索该对象中的所有名称。

Try this: 尝试这个:

Create JSONObject of your file and try to get array of all names and iterate it to get all values. 创建文件的JSONObject并尝试获取所有名称的数组并对其进行迭代以获取所有值。

public static String[] getNames(JSONObject jo) {

        int length = jo.length();
        if (length == 0) {
            return null;
        }
        Iterator i = jo.keys();
        String[] names = new String[length];
        int j = 0;
        while (i.hasNext()) {
            names[j] = (String) i.next();
            j += 1;
        }       
        return names;
    }

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

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