Java 无法解析此json字段

Java 无法解析此json字段,java,json,Java,Json,我可以正确获取其他字段,但可以解析“title”或“title\u full”值。我总是收到一个空字符串。我正在使用org.json库。这就是json。有什么诀窍 try { title = jsonDoc.getString("title_full"); } catch (JSONException e) { log.info("no full title: " + docString);

我可以正确获取其他字段,但可以解析“title”或“title\u full”值。我总是收到一个空字符串。我正在使用org.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}
   }
}

格式化json代码后,问题变得很明显:
title\u full
仅在
thread
节点内可用,非空的
title
也仅在
thread
节点内可用。因此,您必须首先访问
线程
节点,然后访问该节点内的
标题
标题_full

使用org.json库,您可以访问如下字段:

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

格式化json代码后,问题变得很明显:
title\u full
仅在
thread
节点内可用,非空的
title
也仅在
thread
节点内可用。因此,您必须首先访问
线程
节点,然后访问该节点内的
标题
标题_full

使用org.json库,您可以访问如下字段:

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

如果查看json,您将看到“title”和“title_full”字段位于线程字段中。
因此,请尝试读取该字段,然后将该字段解析为一个新的jsonObject,您应该能够获得它们。

如果您查看json,您将看到“title”和“title\u full”字段位于thread字段中。
因此,尝试读取该字段,然后将该字段解析为一个新的jsonObject,您应该能够获得它们。

您的JSON如下所示:

{
     {
    "main": {
        "key": "value",
            },
    },
}
因此,首先获取主json,然后获取密钥

代码应如下所示:

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

JSON中有两个标题值,请在获取之前检查您需要的标题。

您的JSON如下所示:

{
     {
    "main": {
        "key": "value",
            },
    },
}
因此,首先获取主json,然后获取密钥

代码应如下所示:

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

JSON中有两个标题值,请在获取之前检查您需要的标题。

您可以缩进JSON吗?分析结构会更简单。您必须访问嵌套的数据结构,title和full_title不在第一级,您必须遍历嵌套。请看,缩进很明显,问题在于
jsonDoc
中没有
title
title_full
,它们在
线程中
对象您可以缩进json吗?分析结构会更简单。您必须访问嵌套的数据结构,title和full_title不在第一级,您必须遍历嵌套。请看,缩进很明显,问题在于
jsonDoc
中没有
title
title_full
,它们位于
thread
objecttry
jsonDoc.getJSONObject(“thread”).getString(“title\u full”)
try
jsonDoc.getJSONObject(“thread”).getString(“title\u full”)
对我来说,这是最完整、格式最完善的答案。您应该增加一些关于
jsonDoc.get(“main”)
将返回什么的精确性。而真正的
jsonDoc
将是相同类型的结构。这对我来说是最完整/格式最完善的答案。您应该增加一些关于
jsonDoc.get(“main”)
将返回什么的精确性。而真正的
jsonDoc
将是同一类型的结构。