简体   繁体   English

读取Json对象并将所需的值存储在Map中

[英]Reading Json Object and Storing required values in a Map

I have a JSON Object containing the name and attribute. 我有一个包含名称和属性的JSON对象。 I need to store selected values to a map/ custom Object. 我需要将选定的值存储到地图/自定义对象。

below is my JSON Object, where I need to consider values of final and official of Prm and Total Load Amt and Com Load Amt of load . 下面是我的JSON对象,在这里我需要考虑的价值finalofficialPrmTotal Load AmtCom Load Amtload those 4 values I need to add it to a custom Object and use it for Further manipulation. 这4个值需要将其添加到自定义对象中,并用于进一步的操作。

{
  "qObject": {
    "element": [
      {
        "name": "PO",
        "attribute": [
          {
            "name": "PO_DT",
            "value": "2017-01-25"
          },
          {
            "name": "POEN_DT",
            "value": "2017-02-24"
          }
        ]
      },
      {
        "name": "DETAILS",
        "attribute": [
          {
            "name": "ZZ_CD",
            "value": "COM"
          },
          {
            "name": "ZZBO_CD",
            "value": "5"
          },
          {
            "name": "ZZEX_CD",
            "value": "PRI"
          },
          {
            "name": "ZZRE_CD",
            "value": "WST"
          }
        ]
      },
      {
        "name": "Prm",
        "attribute": [
          {
            "name": "Theoritical",
            "value": "0.0000"
          },
          {
            "name": "Final",
            "value": "741.7513"
          },
          {
            "name": "Official",
            "value": "1009.9481"
          }
        ]
      },
      {
        "name": "Load",
        "attribute": [
          {
            "name": "TotalLoadPercentage",
            "value": "27.6900"
          },
          {
            "name": "TotalLoadAmt",
            "value": "268.1968"
          },
          {
            "name": "ComLoadPercentage",
            "value": "5"
          },
          {
            "name": "ComLoadAmt",
            "value": "50.4974"
          }
        ]
      }
    ]
  }
}

I need to store values of "name": "Total Load Amt "name": "Com Load Amt "name": "Final" and "name": "Official" to below Custom Object 我需要在以下“自定义对象"name": "Official"存储以下值: "name": "Total Load Amt "name": "Com Load Amt "name": "Final""name": "Official"

 public Class customObject {
private String totalLoadAmt;
private String comLoadAmt;
private String finalval;
private String official;
}

How to do it in Java? 用Java怎么做? I have thought of Using Map but it didn't work as expected. 我曾考虑过使用地图,但是它没有按预期工作。 Any leads or suggestions will be helpful. 任何线索或建议都会有所帮助。 Thanks in advance. 提前致谢。

You can use Gson class to parse the json. 您可以使用Gson类来解析json。


Gson gson = new Gson();
pcResponseType = gson.fromJson(jsonString,PcResponseType.class);

Following which you'll be able to get whichever fields you want to retrieve and set in your map. 接下来,您将能够获取要检索并在地图中设置的任何字段。

I hope i understood your question and answered appropriately. 希望我能理解您的问题并适当回答。 Hope it helps. 希望能帮助到你。

You can create you custom class as : 您可以按以下方式创建自定义类:

@Getter
@Setter
public class CustomObject {
    private  Map<String, List<Element>> qObject;
}
@Getter
@Setter
public class Element {

    private String name;
    private List<Attribute> attribute;
}

@Getter
@Setter
public class Attribute {

    private String name;
    private String value;
}

Now you can deserialize your json as : 现在,您可以将json反序列化为:

 Gson gson = new Gson();
 CustomObject jsonObject = gson.fromJson(json,CustomObject.class);
 System.out.println(jsonObject);

You need create classes with current field for mapping json string. 您需要使用当前字段创建类以映射json字符串。 Gson using variable type and name for parsing. Gson使用变量类型和名称进行解析。 Setters and getters is optional, you can create them only in the right classes. setter和getter是可选的,您只能在正确的类中创建它们。 If you don't want to read it all =) gist with all code 如果您不想全部阅读=) 包含所有代码的要点


Create class for main json element with field Map qObject for mapping json array: 使用字段Map qObject创建主要json元素的类以映射json数组:

public static class CustomJsonObject {
    private Map<String, List<Element>> qObject;

    // getters and setters
}

Then you need create class for mapping any elements in "element" json array with fields String name and List attribute: 然后,您需要创建类,以使用字符串名称和列表属性字段映射“ element” json数组中的任何元素:

public static class Element {
    private String name;
    private List<Attributes> attribute;

    // getters and setters
}

Then you need create class for mapping any elements in "attribute" json array with fields String name and String value: 然后,您需要创建类,以使用字符串名称和字符串值字段映射“属性” json数组中的任何元素:

public static class Attributes {
    private String name;
    private String value;

    // getters and setters
}

After creating this classes you can write this: 创建此类后,您可以编写以下代码:

CustomJsonObject customJsonObject = new Gson().fromJson(jsonStr, CustomJsonObject.class);

And get class with all json information. 并获取包含所有json信息的类。 Next step its find current data and put it in your class. 下一步,找到当前数据并将其放入您的班级。 You can use stream-api for it, or foreach, if, etc. My full code i put in gist . 您可以使用stream-api,也可以使用foreach,等等。我的完整代码放在gist中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM