首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何解析JSON并只获取一个字段?

如何解析JSON并只获取一个字段?
EN

Stack Overflow用户
提问于 2021-11-18 10:14:57
回答 2查看 68关注 0票数 0

我正在接收我想要解析的JSON:

代码语言:javascript
运行
复制
[
    {
        "id": "f9952c24-1b44-4379-9aef-b10075e93562",
        "sections": [
            {
                "id": "7fe9f47e-9cfe-46c7-9c77-f729b9fb98a4",
                "type": "vehicle",
                "summary": {
                    "duration": 23377,
                    "length": 480501,
                    "baseDuration": 22140
                }
            }
        ]
    }
]

我的Java模型类(由json2pojo生成):

代码语言:javascript
运行
复制
public class HereRoute {
  @SerializedName("routes")
  @Expose
  private List<Route> routes = null;
}

public class Route {
  @SerializedName("id")
  @Expose
  private String id;
  @SerializedName("sections")
  @Expose
  private List<Section> sections = null;
}

public class Section {
  @SerializedName("id")
  @Expose
  private String id;
  @SerializedName("type")
  @Expose
  private String type;
  @SerializedName("summary")
  @Expose
  private Summary summary;
  }

public class Summary {
  @SerializedName("duration")
  @Expose
  private Integer duration;
  @SerializedName("length")
  @Expose
  private Integer length;
  @SerializedName("baseDuration")
  @Expose
  private Integer baseDuration;
}

我只对我想保存在db中的摘要长度感兴趣。如何解析它才能从json中仅获得每个对象的摘要长度?我需要像Gson这样的东西吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-11-18 10:33:25

首先,在您的项目中包含JSON库。

然后像这样遍历你的JSON:

代码语言:javascript
运行
复制
JSONArray firstLevelArrray = new JSONArray(jsonString); // Getting the big array

for(int i = 0 ; i < firstLevelArrray.length(); i++)
{
    Integer length = firstLevelArray.getJSONObject(i) // getting the first object of the array
               .getJSONArray("sections") // Getting the sections array
               .getJSONObject(0) // Getting the first element of the sections array
               .getJSONObject("summary")
               .getInt("length");
               
    // But how sure are you that you won't have a null in there?
}

我还是不会走这条路,这里面有很大的NullPointerException的可能性。您可以随时处理它,并将其视为字段不在其中,但仍然...

票数 2
EN

Stack Overflow用户

发布于 2021-11-18 11:38:27

您可以使用“jackson”进行反序列化。

使用maven包含依赖项

代码语言:javascript
运行
复制
<dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.13.0</version>
</dependency>

使用ObjectMapper对JSON字符串进行反序列化。

代码语言:javascript
运行
复制
ObjectMapper mapper = new ObjectMapper();
data = mapper.readValue(json, Data[].class);

一个完整的工作示例如下所示

代码语言:javascript
运行
复制
import java.io.IOException;

import com.fasterxml.jackson.databind.ObjectMapper;

public class Test {

static String json = "[\r\n"
        + "    {\r\n"
        + "        \"id\": \"f9952c24-1b44-4379-9aef-b10075e93562\",\r\n"
        + "        \"sections\": [\r\n"
        + "            {\r\n"
        + "                \"id\": \"7fe9f47e-9cfe-46c7-9c77- 
f729b9fb98a4\",\r\n"
        + "                \"type\": \"vehicle\",\r\n"
        + "                \"summary\": {\r\n"
        + "                    \"duration\": 23377,\r\n"
        + "                    \"length\": 480501,\r\n"
        + "                    \"baseDuration\": 22140\r\n"
        + "                }\r\n"
        + "            }\r\n"
        + "        ]\r\n"
        + "    }\r\n"
        + "]";
public static void main(String[] args) {
    ObjectMapper mapper = new ObjectMapper();
    Test.Data data[] = null;
    try {
        data = mapper.readValue(json, Data[].class);
    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.println(data[0].sections[0].summary.length);

}

private static class Data {
    String id;
    Sections sections[];

    public Data() {
        super();
    }

    public Data(String id, Test.Sections[] sections) {
        super();
        this.id = id;
        this.sections = sections;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Sections[] getSections() {
        return sections;
    }

    public void setSections(Sections[] sections) {
        this.sections = sections;
    }

}

private static class Sections {
    private String id;
    private String type;
    private Summary summary;

    public Sections() {
        super();
    }

    public Sections(String id, String type, Test.Summary summary) {
        super();
        this.id = id;
        this.type = type;
        this.summary = summary;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Summary getSummary() {
        return summary;
    }

    public void setSummary(Summary summary) {
        this.summary = summary;
    }

}

private static class Summary {
    private Integer duration;
    private Integer length;
    private Integer baseDuration;

    public Summary() {
        super();
    }

    public Summary(Integer duration, Integer length, Integer baseDuration) {
        super();
        this.duration = duration;
        this.length = length;
        this.baseDuration = baseDuration;
    }

    public Integer getDuration() {
        return duration;
    }

    public void setDuration(Integer duration) {
        this.duration = duration;
    }

    public Integer getLength() {
        return length;
    }

    public void setLength(Integer length) {
        this.length = length;
    }

    public Integer getBaseDuration() {
        return baseDuration;
    }

    public void setBaseDuration(Integer baseDuration) {
        this.baseDuration = baseDuration;
    }

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

https://stackoverflow.com/questions/70018007

复制
相关文章

相似问题

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