在Java中将JSON对象转换为简单变量 [英] Convert JSON object to simple variables in Java

查看:511
本文介绍了在Java中将JSON对象转换为简单变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常繁重的JSON,其中包含许多参数,我想将它们转换为仅包含其中一些参数的Java对象。

I have a very heavy JSON with lots of parameters which I want to convert to a Java object containing just a few of them.

直接转换,例如

DataObject obj = gson.fromJson(br, DataObject.class);

不是一个选项。

如何访问对象中的单个字段( date completion_date 下的属性下输入
JSON示例:

How can I access individual fields within objects (just value under date and type under attributes under completion_date)? JSON example:

{"results":[
    {"date":{
        "attributes":{
        "type":null},
        "value":"December 13, 2010"},
    "is_structured":"No",
    "completion_date":{
        "attributes":{
            "type":"Anticipated"},
        "value":"March 2016"},
     ....


推荐答案

如果您不想直接将输入转换为您可以从输入中创建 JSONObject 的预期对象,如

If you don't want to directly convert your input to the expected Object you could create a JSONObject from your input like

JSONObject root = new JSONObject(input);

然后从那里导航到你需要的属性,例如:

and then navigate from there exactly to the attribute you need e.g.:

root.getJSONArray("results").getJSONObject(0).getString("is_structured");

(在<$下收到 c $ c> date )

(receive value under date)

root.getJSONArray("results").getJSONObject(0).getJSONObject("date").getString("value");

这篇关于在Java中将JSON对象转换为简单变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆