简体   繁体   English

无法解析此json字段

[英]Failure to parse this json field

I can get other fields correctly, but can parse the "title" or "title_full" values. 我可以正确获取其他字段,但是可以解析“ title”或“ title_full”值。 I always receives an empty string. 我总是收到一个空字符串。 I am using the org.json library. 我正在使用org.json库。 This is the json. 这是json。 What's the trick? 诀窍是什么?

try {
                title = jsonDoc.getString("title_full");
            } catch (JSONException e) {
                log.info("no full title: " + docString);
            }

{
  "organizations": [],
  "uuid": "d0adc516c9012113774557365f9847da99b228e7",
  "thread": {
    "site_full": "www.fark.com",
    "main_image": "http://img.fark.net/images/cache/orig/5/51/fark_514Jh7VFpynQw4MyN2xcK1jwCxk.png?t=RQrnhq8EGZiUuElMitgLOQ&f=1488776400",
    "site_section": "http://www.fark.com/discussion/",
    "section_title": "FARK.com: Discussion links",
    "url": "http://www.fark.com/comments/9500577/I-want-to-support-work-that-NY-Times-Washington-Post-are-doing-I-can-only-afford-one-subscription-Who-do-you-recommend-I-throw-my-support-to?cpp=1",
    "country": "US",
    "domain_rank": 3382,
    "title": "(9500577) I want to support the work that the NY Times and Washington Post are doing. I can only afford one subscription. Who do you recommend I throw my support to?",
    "performance_score": 0,
    "site": "fark.com",
    "participants_count": 31,
    "title_full": "FARK.com: (9500577) I want to support the work that the NY Times and Washington Post are doing. I can only afford one subscription. Who do you recommend I throw my support to?",
    "spam_score": 0.0,
    "site_type": "discussions",
    "published": "2017-03-03T12:00:00.000+02:00",
    "replies_count": 2,
    "uuid": "67213179a24931106e75cd588386bd30fb3bbdc8"
  },
  "author": "EbolaNYC",
  "url": "http://www.fark.com/comments/9500577/I-want-to-support-work-that-NY-Times-Washington-Post-are-doing-I-can-only-afford-one-subscription-Who-do-you-recommend-I-throw-my-support-to?cpp=1#c107765048",
  "ord_in_thread": 1,
  "title": "",
  "locations": [], 
  "entities": {
    "persons": [],
    "locations": [],
    "organizations": []
  },
  "highlightText": "",
  "language": "english",
  "persons": [], 
  "text": "dionysusaur : Either the NY Post or the WA Times.\nOnly asshats read the NY Post.",
  "external_links": [],
  "published": "2017-03-03T15:58:00.000+02:00",
  "crawled": "2017-03-03T17:05:26.049+02:00", 
  "highlightTitle": "",
  "social": {
    "gplus": {"shares": 0}, 
    "pinterest": {"shares": 0}, 
    "vk": {"shares": 0}, 
    "linkedin": {"shares": 0},
    "facebook": {"likes": 0, "shares": 0, "comments": 0}, 
    "stumbledupon": {"shares": 0}
   }
}

Your JSON seems like follows: 您的JSON如下所示:

{
     {
    "main": {
        "key": "value",
            },
    },
}

So, First fetch the main json and then the key. 因此,首先获取主要json,然后获取密钥。

Code should be like as follows: 代码应如下所示:

String something = jsonDoc.get("main").get("key").toString();

There are two title values in your JSON, Do check which title you need before fetching. JSON中有两个标题值,请在提取之前检查所需的标题。

After I formatted the json code, the problem becomes obvious: title_full is only available inside the thread node, and a non-empty title is also only inside the thread node. 在格式化json代码之后,问题变得很明显: title_full仅在thread节点内可用,并且非空title也仅在thread节点内。 So you'll first have to access the thread node and then access title and title_full inside that node. 因此,您首先必须访问thread节点,然后访问该节点内的titletitle_full

Using the org.json library, you can access the fields like this: 使用org.json库,您可以访问如下字段:

String fullTitle = jsonDoc.getJSONObject("thread").getString("title_full");

If you take a look at the json you will see that the "title" and "title_full" fields are in the thread field. 如果您查看json,您会看到“ title”和“ title_full”字段在线程字段中。 So try reading that field and then parsing the filed into a new jsonObject and you should be able to get them. 因此,尝试读取该字段,然后将文件解析为新的jsonObject,您应该能够获取它们。

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

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