I'm new to JSON. I'm trying to assign value (in key value pairs) as array of strings using GSON.
The JSON should look like as below:
{ "name": "path", "value": [ "/my-path" ,"/my-path2","/newpath"] }
How can I achieve this?
Thanks.
Even thoe I will hardly recommend you to use POJOS, gson is flexible enough to allow you to do what you want:
JsonObject jo = new JsonObject();
jo.addProperty("name", "path");
JsonArray jsonArray = new JsonArray();
jsonArray.add("my-path");
jsonArray.add("my-path2");
jsonArray.add("my-new-path");
jo.add("value", jsonArray);
System.out.println(jo);
Related
i have a json-object named jsonObject
{
"action":"Read",
"infos":[
{
"value":0.0350661,
"key":"first"
}
]
}
i wanna to print the json-object to with the following form
{"action":"Read","infos":[{"value":0.0350661,"key":"first"}]}
if i use jsonObject.toString() method i will get
{"action":"Read","infos":"[{\"value\":0.0350661,\"key\":\"first\"}]"}
if i use StringEscapeUtils.unescapeJava(jsonObject.toString()) method i will get
{"action":"Read","infos":"[{"value":0.0350661,"key":"first"}]"}
if i use jackson mapper with the following code
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
String jsonString = mapper.writeValueAsString(getDebugInfo())
i will get jsonString as
{"nameValuePairs":{"action":"Read","infos":[{"value":0.0350661,"key":"first"}]}}
is there any solution to get the desired output json-string?
JSON Structure
You have that as an object, that is why quotes are not present there.
In your example, an array object is present, at the Json structure.
Code/Java
While printing at Console, the json body's every Key & Value toString() are referred .
That is why the Double Quotes present, as Strings are getting used!
Here I have tried the below code using GSON library, and it is printing me the correct json as shown above.
public static void main ( String [] args ) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("action", "Read");
JsonArray jsonArr = new JsonArray();
JsonObject jsonObject2 = new JsonObject();
jsonObject2.addProperty("value", 0.0350661);
jsonObject2.addProperty("key", "first");
jsonArr.add(jsonObject2);
jsonObject.add("infos", jsonArr);
String jsonString = jsonObject.toString();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonElement json = gson.fromJson(jsonString,JsonElement.class);
String jsonInString = gson.toJson(json);
System.out.println(jsonInString);
}
OUTPUT:
{
"action": "Read",
"infos": [
{
"value": 0.0350661,
"key": "first"
}
]
}
Even if I am forming the jsonObject using org.json, and simple printing it using System.out.println(jsonObject.toString()); on console, m getting the result like this.
{"action":"Read","infos":[{"value":0.0350661,"key":"first"}]}
So here, not sure how you have formed your jsonObject.
I have this json file that I'm trying to parse in my program.
{
"items": [{
"0": {
"item_name":"Test Item",
"item_rarity":2,
"item_material":"STICK",
"required_level":1,
"min_damage":100.0,
"max_damage":200.0,
"item_set":"The cool kids",
"attributes":[{"name":"lifesteal","modifier":20}]
},
"1": {
"item_name":"Test Item",
"item_rarity":2,
"item_material":"STICK",
"required_level":1,
"min_damage":100.0,
"max_damage":200.0,
"item_set":"The cool kids",
"attributes":[{"name":"lifesteal","modifier":20}]
}
}]
}
I am printing the JSON string, but instead of getting the individual objects (0, then 1, then 2, etc...) it only gets the whole array every time I print it out.
Object obj = jsonParser.parse(new FileReader(new File(ValaCraft.getInstance().getDataFolder() + "/test.json")));
JSONObject jsonObject = (JSONObject) obj;
JSONArray items = (JSONArray) jsonObject.get("items");
for (int i = 0; i < items.size(); i++) {
JSONObject item = (JSONObject) items.get(i);
System.out.print(item.toString());
}
Anybody have an idea on how to parse this file (without GSON, attributes is a custom class and I found it complicated to use the auto parse that gson comes with).
What did you find troubling with GSON?
If you pass it to the gson.fromJSON as a JSONObject class, it should work, and you'll be able to get data from the JSON object.
Gson gson = new Gson();
JsonObject jsonFile = gson.fromJson(file.json, JsonObject.class);
Then you can call
JsonArray array = jsonFile.get("items").getAsJsonArray();
Then to grab the attributes from the first element of the array.
array.get(0).getAsJsonObject().get("attributes").getAsJsonArray();
I have two JsonObject having same key but different value.
I want to merge both JsonObject with same key in another JsonObject.
JSONObject a = new JSONObject("{\"data\": [ {\"empId\": 1,\"deptId\": 2},{\"empId\": 3,\"deptId\": 4}]}");
JSONObject b = new JSONObject("{\"data\": [ {\"empId\": 7,\"deptId\": 8},{\"empId\": 9,\"deptId\": 10}]}");
The result should be like this.
{\"data\": [ {\"empId\": 1,\"deptId\": 2},{\"empId\": 3,\"deptId\": 4},{\"empId\": 7,\"deptId\": 8},{\"empId\": 9,\"deptId\": 10}]}
Please Let me know how to do this.
Use JSONArray for store multiple JSONObject. Using this no need to worry to assign key for this JSONObject e.g.
JSONObject a = new JSONObject("{\"data\": [ {\"empId\": 1,\"deptId\": 2},{\"empId\": 3,\"deptId\": 4}]}");
JSONObject b = new JSONObject("{\"data\": [ {\"empId\": 7,\"deptId\": 8},{\"empId\": 9,\"deptId\": 10}]}");
Edit
JSONArray jArr_A= a.getJSONArray("data");
JSONArray jArr= new JSONArray();
for(int i=0;i<jArr_A.length();i++){
jArr.put(jArr_A.getJSONObject(i));
}
Now for another object
jArr_A= b.getJSONArray("data");
for(int i=0;i<jArr_A.length();i++){
jArr.put(jArr_A.getJSONObject(i));
}
now check the length of your jArr object has it double.
JSONObject mainJson = new JSONObject();
mainJson.put("data", jArr);
How can I create a JSONArray, since creating a JSONObject is quite simple:
JSONObject j = new JSONObject();
j.put("key",value);
Right now I can put another string in the JSONObject, or a string representation of a JSONObject.
But how can I create a JSONArray and insert it to the JSONObject?
But how can I create a JSONArray and insert it to the JSONObject?
You can create JSONArray same like you have tried to create JSONObject.
Creating time:
For example:
JSONArray myArray = new JSONArray();
JSONObject j = new JSONObject();
j.put("key",value);
j.put("array",myArray);
Retrieving time:
you can fetch the value of String or JSONObject or any by their key name. For example:
JSONArray myArray = objJson.getJSONArray("array");
You can do it like:
String[] data = {"stringone", "stringtwo"};
JSONArray json = new JSONArray(Arrays.toString(data));
Or, create a JSONArray object and use the put method(s) to add any Strings you want. To output the result, just use the toString() method.
Why dont you use Gson library its very easy to convert any object into json array, json object
Download Gson library then use like
Gson gson=new Gson();
String json=gson.toJson(object);
if Object is of List object it will create json array
Gson gson = new Gson();
reverse parsing for array --
listObject = gson.fromJson(json,
new TypeToken<List<ClassName>>() {
}.getType());
for single object
object = gson.fromJson(json, ClassName.class);
I'm trying to encode this string for a POST request. Can anyone tell me how I can encode
{"jsonrpc": "2.0", "method": "Files.GetSources", "params":{"media":"music"}, "id": 1}
So far I have
JSONOjbect obj = new JSONObject();
obj.put("jsonrpc", "2.0");
obj.put("method", "Files.GetSources");
But I'm not sure how to put in the rest - can anyone help?
If you're asking how you'd put the nested params object in there, you'd probably do:
JSONObject params = new JSONObject();
params.put("media", "music");
obj.put("params", params);
To use an array (per your comments below), you'd do something like this:
JSONArray properties = new JSONArray();
properties.put("resume");
properties.put("genre");
properties.put("studio");
...
JSONObject params = new JSONObject();
params.put("properties", properties);
obj.put("params", params);
JSONOjbect obj = new JSONObject().put("jsonrpc", "2.0")
.put("method", "Files.GetSources").put("id", 1)
.put("params", new JSONObject().put("media", "music"));
Chaining .put() like this is possible because put() returns the object it was called on - for this exact purpose.
You have two choices. You can create another object that holds "media":"music" and then put that in the original JSONObject or you can just pass this whole string into the JSONObject constructor and have it parse it for you.
Well, if you do have the string before hand, you can simply do
JSONObject object = JSONObject.getJSONObject("{\"jsonrpc\": \"2.0\", \"method\": \"Files.GetSources\", \"params\":{\"media\":\"music\"}, \"id\": 1}");