【问题标题】:How to retrieve value from a nested JSON如何从嵌套的 JSON 中检索值
【发布时间】:2018-05-28 11:15:15
【问题描述】:

我有以下 JSON。

数据:

{
posts: [{
    thread: {
        uuid: "8d4d59ad10e0b5df2a9f3cac30f1a6b96e809233",
        url: "http://www.esquire.com/entertainment/news/a53604/deadpool-2-teaser/",
        site_full: "www.esquire.com",
        site: "esquire.com",
        site_section: "",
        site_categories: [
            "news",
            "entertainment",
            "education"
        ],
        section_title: "",
        title: "The Deadpool 2 Teaser Just Dropped and It's Great",
        title_full: "The Deadpool 2 Teaser Just Dropped and It's Great",
        published: "2017-03-05T00:45:28.810+02:00",
        replies_count: 0,
        participants_count: 0,
        site_type: "news",
        country: "US",
        spam_score: 0.127,
        main_image: "http://esq.h-cdn.co/assets/17/09/1600x800/landscape-1488661852-            deadpool2-630x400-copy.jpg",
        performance_score: 9,
        domain_rank: 1790,
        social: {
            facebook: {
                likes: 999,
                comments: 0,
                shares: 999
            },
            gplus: {
                shares: 0
            },
            pinterest: {
                shares: 0
            },
            linkedin: {
                shares: 0
            },
            stumbledupon: {
                shares: 0
            },
            vk: {
                shares: 0
            }
        }
    },
    uuid: "XXXXX",
    url: "http://www.esquire.com/entertainment/news/a53604/deadpool-2-teaser/",
    ord_in_thread: 0,
    author: "Peter Wade",
    published: "2017-03-05T00:45:28.810+02:00",
    title: "The Deadpool 2 Teaser Just Dropped and It's Great",
    text: "The Deadpool 2 Teaser Just Dropped and It's Great "
    Anyone know the number to 911 ? " Most Popular By Peter Wade Mar 4, 2017 The Deadpool 2 teaser trailer that has been playing exclusively in theaters before Logan was officially released by Ryan Reynolds on Saturday. As expected, Deadpool is still a second-rate hero and a fan of butt jokes, struggling to even get his super hero suit on Superman-style in a phone booth while a senior citizen is being mugged nearby. Related Story Ryan Reynolds Was Kind to Drugged-Up Deadpool Fan Advertisement - Continue Reading Below "
    Somebody save me!Is there anybody in this world who can help me,
    for God 's sake?" the man screams helplessly while Reynolds'
    Deadpool smashes his ass against the booth 's glass. GIF Most Popular Then Stan Lee appeared, and things went downhill from there. If you look closely, another clue lurks in the form of graffiti on the side of the phone booth that reads, "Nathan Summers, Cumming Soon!" The name is a reference to X-Men character Cable, a.k.a. Nathan Summers, hinting that he may be Deadpool'
    s foe this time around.Deadpool 2 is a little less than a year away,
    with a scheduled release date of March 2,
    2018. Related Story ",
    highlightText : "",
    highlightTitle: "",
    language: "english",
    external_links: [],
    entities: {
        persons: [{
                name: "reynolds",
                sentiment: "none"
            },
            {
                name: "deadpool",
                sentiment: "none"
            },
            {
                name: "peter wade mar",
                sentiment: "none"
            },
            {
                name: "ryan reynolds",
                sentiment: "none"
            },
            {
                name: "stan lee",
                sentiment: "none"
            },
            {
                name: "nathan summers",
                sentiment: "none"
            }
        ],
        organizations: [{
            name: "deadpool",
            sentiment: "positive"
        }],
        locations: [{
            name: "logan",
            sentiment: "none"
        }]
    },
    rating: null,
    crawled: "2017-03-05T00:45:28.810+02:00"
}],
totalResults: 3411,
moreResultsAvailable: 3410,
next: "/filterWebContent?token=XXXXX-XXXX-XXXX-    XXXXX&format=json&ts=1488464225907&q=deadpool&sort=social.facebook.likes&from=1&    sort=desc&size=1",
requestsLeft: 999911
}

问题:

我正在尝试从这个 JSON 中提取一些字段。但是有些字段是高度嵌套的,有些是单嵌套的。

例如,使用“帖子”字段,我可以获得标题、作者、语言键的值。但是,当我尝试获取国家、位置、人员、组织、情绪的值时,使用以下代码,我得到所有这些值。我的代码sn-p如下。

JsonArray postArray = result.getAsJsonObject().getAsJsonArray("posts");
System.out.println(postArray);
for(JsonElement o  : postArray) {
    System.out.println(o.getAsJsonObject().get("title"));  
    System.out.println(o.getAsJsonObject().get("author")); 
    System.out.println(o.getAsJsonObject().get("language"));   
    System.out.println(o.getAsJsonObject().get("country"));  
    System.out.println(o.getAsJsonObject().get("locations"));   
    System.out.println(o.getAsJsonObject().get("persons"));
    System.out.println(o.getAsJsonObject().get("organizations"));
    System.out.println(o.getAsJsonObject().get("sentiment"));
    System.out.println ("--------------------------------------");
}           

如何打印与所有字段/键对应的值?

【问题讨论】:

  • 你的 json 在哪里?
  • 而不是多次调用o.getAsJsonObject(),只需在循环开始时调用一次,并将其存储在一个变量中
  • 你的 json 格式错误
  • 对 Json 无能为力。这是我从其他来源得到的。

标签: java json parsing


【解决方案1】:

您可以在您的 java 对象上使用递归。

  • 检查您的值是 JSON 对象。如果是,则进行递归调用
  • Else 打印带值的键

【讨论】:

    猜你喜欢
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    相关资源
    最近更新 更多