首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从嵌套的JSON中检索值

如何从嵌套的JSON中检索值
EN

Stack Overflow用户
提问于 2018-05-28 19:15:16
回答 1查看 49关注 0票数 -1

我有一个下面的JSON。

数据:

代码语言:javascript
复制
{
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中提取一些字段。但是,有些字段是高度嵌套的,有些字段是单嵌套的。

例如,使用“post”字段,我可以获得标题、作者、语言键的值。但是,当我尝试获取国家/地区、地点、人员、组织、情绪的值时,使用以下代码时,所有这些值都为空。我的代码片段如下所示。

代码语言:javascript
复制
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 ("--------------------------------------");
}           

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

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50565126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档