有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法解析此json字段

我可以正确获取其他字段,但可以解析“title”或“title_full”值。我总是收到一个空字符串。我正在使用组织。json库。这是json。诀窍是什么

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}
   }
}

共 (2) 个答案

  1. # 1 楼答案

    在格式化json代码后,问题变得显而易见: title_full仅在thread节点内可用,非空的title也仅在thread节点内可用。因此,首先必须访问thread节点,然后访问该节点内的titletitle_full

    使用组织。json库,您可以访问如下字段:

    String fullTitle = jsonDoc.getJSONObject("thread").getString("title_full");
    
  2. # 2 楼答案

    如果你看一下json,你会发现“title”和“title_full”字段在thread字段中。 因此,尝试读取该字段,然后将该字段解析为一个新的jsonObject,您应该能够获得它们