I have the following json, how can i convert it to arrayList?
"data": [{
"label": "Venezuela",
"value": "290"
}, {
"label": "Saudi",
"value": "260"
}, {
"label": "Canada",
"value": "180"
}, {
"label": "Iran",
"value": "140"
}, {
"label": "Russia",
"value": "115"
}, {
"label": "UAE",
"value": "100"
}, {
"label": "US",
"value": "30"
}, {
"label": "China",
"value": "30"
}]
}
You need to convert it manually iterate each json and assign it to an object then you can add it to a collection.
or even you can use Gson Library take a look at the following, gson can map your attribute into Java Object.
JsonElement yourJson = mapping.get("data");
Type listType = new TypeToken<List<YourModel>>() {}.getType(); //YourModel should contains the same attributes which the json have (Exactly)
List<String> yourList = new Gson().fromJson(yourJson, listType);
Related
I have Json schema as input I want to know whether there is a method by which I can generate sample json output. Do we have any such library in java which I can use?
{
"title": "InputSchema",
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"age": {
"description": "Age in years",
"type": "integer",
}
},
"required": ["firstName", "lastName"]
}
output =>
{
"firstName" : "FirstName",
"lastName" : "LastName"
}
How to get a new list from the list of objects?
I need a new list of objects to POST request
this list of objects i get from response:
{
"success": true,
"body": {
"users": [
{
"type": "unknown",
"data": [
{
"id": "8",
"firstName": "Jackson",
"lastName": "Baker",
"group": "false"
},
{
"id": "11",
"firstName": "Charlotte",
"lastName": "Garcia",
"group": "false"
},
{
"id": "7",
"firstName": "Henry",
"lastName": "Thompson",
"group": "false"
},
{
"id": "24",
"firstName": "Elijah",
"lastName": "Miller",
"group": "false"
}
]
}
]
}
}
I need to form a new object from response:
{
"success": true,
"body": {
"users": [
{
"type": "unknown",
"data": [
{
"id": "8",
"group": "false"
},
{
"id": "11",
"group": "false"
},
{
"id": "7",
"group": "false"
},
{
"id": "24",
"group": "false"
}
]
}
]
}
}
This new list of object I need to post a request.
I have a JSON Schema, but i don't know how i can make this structure.
My problem is that I can't get the values from fields. I'm using the path "body.users.data.find {it.id} .id" but it finds the id array but I need a users list with values from two fields.
As the users node and data node are all list type, you need use two loop:
import groovy.json.JsonSlurper
// text is your json response text
def result = new JsonSlurper().parseText(text)
result.body.users.each{it.data.each{it.remove("firstName");it.remove("lastName")}}
// result is the what you wanted
println(result)
This is my sample Json
{
"State": {
"version": "1",
"SName": "Test",
"shippingDetails": {
"Address1": "AP",
"ZipCode": "1236"
},
"Directions": {
"routes": [
{
"taxAmount": "0.0",
"Quantity": "5",
"bounds": {
"SerialVersion": [
{
"text": "1.7 km",
"value": "1729",
"time": "02633"
},
{
"text": "1.9 km",
"value": "1829",
"time": "02353"
},
{
"text": "17 km",
"value": "1059",
"time": "02133"
}
]
}
}
]
}
}
}
I want to update SName, ZipCode,taxAmount,Quantity, and text[1] values
are there any way to do this. I am taking JSON in a file and update tags are taking into HashMap
JSONObject jsonObject = new JSONObject("Your_JSON_String");
JSONObject jsonObjectForState = jsonObject.getJSONObject(“State”);
jsonObjectForState.put("Sname", "New_Value_Here");
put(...) will replace the current value with new value. Similarly, you can update the other values. Once you are done, you can convert it back using:
jsonObject.toString();
And write it back to the file.
I have a json something like this below .I want to read it and add 2 more attribute ti it like country and state
[
{
"id": "123",
"testname": "test123",
"name": "John Doe",
"active": true,
"type": "test6"
}
{
"id": "456",
"testname": "test564",
"name": "Ship Therasus",
"active": true,
"type": "test7"
}
]
Resulting json
[
{
"id": "123",
"testname": "test123",
"name": "John Doe",
"active": true,
"type": "test6",
"country":"USA",
"state":"KA"
}
{
"id": "456",
"testname": "test564",
"name": "Ship Therasus",
"active": true,
"type": "test7",
"country":"UK",
"state":"MA"
}
]
I am doing something like this I tried JSONObject but no output.
JSONArray xmlJSONObj2 = new JSONArray(output);
System.out.println("Output from Server .... \n"+xmlJSONObj2.get(0));
you can use for loop to iterate over JSONArray, Once you have jsonObject use put for extra attribute like this
for(int i=0;i<jsonArray.length();i++){
JSONObject json = jsonArray.getJSONObject(i);
// do this for all remaining field
if(json.has("id")){
String id = json.get("id").toString();
}
// finally put extra attributes to that jsonobject
json.put("country", "USA");
json.put("state", "KA")
}
missed commas after "type":"test6"
{
"id": "123",
"testname": "test123",
"name": "John Doe",
"active": true,
"type": "test6" <------
"country":"USA",
"state":"KA"
}
I got json[] from UI. I have to convert it into Json object how can I convert it with gson. An object which is received is like that:
[{"name":"name","value":"value"},{"name":"first","value":"100"},{"name":"hor","value":"95"},{"name":"conf","value":"95"}],[{"name":"vaRType","value":"INCRE"},{"name":"per","value":"100"},{"name":"hor","value":"95"},{"name":"conf","value":"95"}]
I used JsonObject obj = new JsoParser().parse(jsonStrnig).getAsJsonObject();
but it throws an exception.
Because it is not valid json object.
Your JSON should be the following as currently it's invalid...
[
[
{
"name": "name",
"value": "value"
},
{
"name": "first",
"value": "100"
},
{
"name": "hor",
"value": "95"
},
{
"name": "conf",
"value": "95"
}
],
[
{
"name": "vaRType",
"value": "INCRE"
},
{
"name": "per",
"value": "100"
},
{
"name": "hor",
"value": "95"
},
{
"name": "conf",
"value": "95"
}
]
]