JSON对象。数组和变量具有相同的名称。怎么解析呢?

时间:2014-03-21 06:43:37

标签: java json parsing

我的JSON格式低于此。需要解析它。使用下面的代码来解析它。但正如你在我的json中看到的那样,有一个变量名称命中,一个数组名为Hits。所以鳕鱼首先读“点击”并给出错误它不是一个json数组。

请建议如何解析它。

JSONObject rootObject = new JSONObject(fullline);
        JSONArray rows = rootObject.getJSONArray("hits");

        for (int i = 0; i < rows.length(); i++) {
            JSONObject row = rows.getJSONObject(i);
            JSONArray elements = row.getJSONArray("_source");

            for (int j = 0; j < elements.length(); j++) {
            JSONObject element = elements.getJSONObject(j);
            JSONObject publisher = element.getJSONObject("publisher");
            JSONObject language = element.getJSONObject("language");

            JSONObject title = element.getJSONObject("title");

            System.out.println("publisher: " + publisher.toString());
            System.out.println("title: " + title.toString());
            }
        }

JSON是:

{
    "hits": {
        "hits": [
            {
                "_score": 1.4500864, 
                "_type": "journal", 
                "_id": "8f5e8900ffc7431f856ba063a8f1c64c", 
                "_source": {
                    "index": {
                        "publisher": [
                            "Asian Network for Scientific Information"
                        ], 
                        "schema_subject": [
                            "LCC:Agriculture", 
                            "DOAJ:Agriculture and Food Sciences", 
                            "LCC:Plant culture", 
                            "DOAJ:Plant Sciences"
                        ], 
                        "language": [
                            "English"
                        ], 
                        "classification": [
                            "Plant culture", 
                            "Agriculture and Food Sciences", 
                            "Agriculture", 
                            "Plant Sciences"
                        ], 
                        "title": [
                            "Asian Journal of Crop Science"
                        ], 
                        "country": "Pakistan", 
                        "issn": [
                            "2077-2041", 
                            "1994-7879"
                        ], 
                        "homepage_url": "http://scialert.net/jindex.php?issn=1994-7879", 
                        "schema_code": [
                            "LCC:SB1-1110", 
                            "LCC:S"
                        ], 
                        "subject": [
                            "Agriculture and Food Sciences", 
                            "genetics", 
                            "plant physiology", 
                            "Plant culture", 
                            "production", 
                            "Plant Sciences", 
                            "breeding", 
                            "Agriculture"
                        ]
                    }, 
                    "last_updated": "2014-03-09T13:31:38Z", 
                    "admin": {
                        "owner": "16823974", 
                        "in_doaj": true
                    }, 
                    "created_date": "2009-04-30T13:50:33Z", 
                    "id": "8f5e8900ffc7431f856ba063a8f1c64c", 
                    "bibjson": {
                        "publisher": "Asian Network for Scientific Information", 
                        "author_pays": "Y", 
                        "language": [
                            "English"
                        ], 
                        "title": "Asian Journal of Crop Science", 
                        "author_pays_url": "http://scialert.net/guide.php?issn=1994-7879", 
                        "country": "PK", 
                        "oa_start": {
                            "volume": "1", 
                            "number": "1", 
                            "year": "2009"
                        }, 
                        "keywords": [
                            "plant physiology", 
                            "production", 
                            "genetics", 
                            "breeding"
                        ], 
                        "link": [
                            {
                                "url": "http://scialert.net/jindex.php?issn=1994-7879", 
                                "type": "homepage"
                            }
                        ], 
                        "provider": "Asian Network for Scientific Information", 
                        "active": true, 
                        "oa_end": {}, 
                        "identifier": [
                            {
                                "type": "pissn", 
                                "id": "1994-7879"
                            }, 
                            {
                                "type": "eissn", 
                                "id": "2077-2041"
                            }
                        ], 
                        "subject": [
                            {
                                "scheme": "LCC", 
                                "term": "Plant culture", 
                                "code": "SB1-1110"
                            }, 
                            {
                                "scheme": "LCC", 
                                "term": "Agriculture", 
                                "code": "S"
                            }, 
                            {
                                "term": "Plant Sciences", 
                                "scheme": "DOAJ"
                            }, 
                            {
                                "term": "Agriculture and Food Sciences", 
                                "scheme": "DOAJ"
                            }
                        ]
                    }
                }, 
                "_index": "doaj"
            }, 
            {
                "_score": 1.4500864, 
                "_type": "journal", 
                "_id": "90287ce3214140948d1fc371793da58c", 
                "_source": {
                    "index": {
                        "publisher": [
                            "Bentham open"
                        ], 
                        "schema_subject": [
                            "LCC:Medicine", 
                            "DOAJ:Health Sciences", 
                            "LCC:Internal medicine", 
                            "DOAJ:Medicine (General)", 
                            "DOAJ:Neurology", 
                            "LCC:Neurology. Diseases of the nervous system", 
                            "LCC:Neurosciences. Biological psychiatry. Neuropsychiatry"
                        ], 
                        "language": [
                            "English"
                        ], 
                        "classification": [
                            "Internal medicine", 
                            "Medicine (General)", 
                            "Health Sciences", 
                            "Neurosciences. Biological psychiatry. Neuropsychiatry", 
                            "Neurology", 
                            "Neurology. Diseases of the nervous system", 
                            "Medicine"
                        ], 
                        "title": [
                            "Open Neurology Journal"
                        ], 
                        "country": "United States", 
                        "issn": [
                            "1874-205X"
                        ], 
                        "homepage_url": "http://www.benthamscience.com/open/toneuj/index.htm", 
                        "schema_code": [
                            "LCC:RC321-571", 
                            "LCC:R", 
                            "LCC:RC31-1245", 
                            "LCC:RC346-429"
                        ], 
                        "subject": [
                            "Internal medicine", 
                            "Medicine (General)", 
                            "Health Sciences", 
                            "Neurosciences. Biological psychiatry. Neuropsychiatry", 
                            "Neurology", 
                            "Neurology. Diseases of the nervous system", 
                            "Medicine", 
                            "neurological disorders"
                        ]
                    }, 
                    "last_updated": "2014-03-09T13:31:38Z", 
                    "admin": {
                        "owner": "18740707", 
                        "in_doaj": true
                    }, 
                    "created_date": "2008-05-16T17:41:22Z", 
                    "id": "90287ce3214140948d1fc371793da58c", 
                    "bibjson": {
                        "publisher": "Bentham open", 
                        "author_pays": "Y", 
                        "language": [
                            "English"
                        ], 
                        "title": "Open Neurology Journal", 
                        "author_pays_url": "http://www.benthamscience.com/open/toneuj/MSandI.htm", 
                        "country": "US", 
                        "oa_start": {
                            "volume": "1", 
                            "year": "2007"
                        }, 
                        "keywords": [
                            "neurological disorders"
                        ], 
                        "link": [
                            {
                                "url": "http://www.benthamscience.com/open/toneuj/index.htm", 
                                "type": "homepage"
                            }
                        ], 
                        "provider": "Bentham open", 
                        "active": true, 
                        "oa_end": {}, 
                        "identifier": [
                            {
                                "type": "pissn", 
                                "id": "1874-205X"
                            }
                        ], 
                        "subject": [
                            {
                                "scheme": "LCC", 
                                "term": "Neurology. Diseases of the nervous system", 
                                "code": "RC346-429"
                            }, 
                            {
                                "scheme": "LCC", 
                                "term": "Neurosciences. Biological psychiatry. Neuropsychiatry", 
                                "code": "RC321-571"
                            }, 
                            {
                                "scheme": "LCC", 
                                "term": "Internal medicine", 
                                "code": "RC31-1245"
                            }, 
                            {
                                "scheme": "LCC", 
                                "term": "Medicine", 
                                "code": "R"
                            }, 
                            {
                                "term": "Neurology", 
                                "scheme": "DOAJ"
                            }, 
                            {
                                "term": "Medicine (General)", 
                                "scheme": "DOAJ"
                            }, 
                            {
                                "term": "Health Sciences", 
                                "scheme": "DOAJ"
                            }
                        ]
                    }
                }, 
                "_index": "doaj"
            }
        ], 
        "total": 9744, 
        "max_score": 1.4500864
    }, 
    "_shards": {
        "successful": 5, 
        "failed": 0, 
        "total": 5
    }, 
    "took": 3, 
    "timed_out": false
}

0 个答案:

没有答案