Reputation: 450
I have a JSON Object containing the name and attribute. I need to store selected values to a map/ custom Object.
below is my JSON Object, where I need to consider values of
final
andofficial
ofPrm
andTotal Load Amt
andCom Load Amt
ofload
. those 4 values I need to add it to a custom Object and use it for Further manipulation.
{
"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
public Class customObject {
private String totalLoadAmt;
private String comLoadAmt;
private String finalval;
private String official;
}
How to do it in Java? I have thought of Using Map but it didn't work as expected. Any leads or suggestions will be helpful. Thanks in advance.
Upvotes: 0
Views: 2218
Reputation: 102
You need create classes with current field for mapping json string. Gson using variable type and name for parsing. Setters and getters is optional, you can create them only in the right classes. 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:
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:
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:
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. 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.
Upvotes: 1
Reputation: 461
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 :
Gson gson = new Gson();
CustomObject jsonObject = gson.fromJson(json,CustomObject.class);
System.out.println(jsonObject);
Upvotes: 1
Reputation: 518
You can use Gson
class to parse the 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.
Upvotes: 1