Accessing JSON array elements in java - java

I have a json file and have used simple.json jar to parse the elements. I could parse the elements successfully. But what I want is that if my json file has three elements by same name, then I want to print each name only when their index is called.
get() prints out all the elements of that name.
Please help!
Following is the json file:
{
"nodes":
[
{
"node":"1",
"ipaddr":"127.0.0.1",
"port":"8443",
"mgport":"9000"
},
{
"node":"2",
"ipaddr":"127.0.0.1",
"port":"8556",
"mgport":"9000"
},
{
"node":"3",
"ipaddr":"127.0.0.1",
"port":"8000",
"mgport":"9000"
}
]
}
I need to retrive only one port value rather than all the values.

I have successfully used the JSON Processing library to do something similar to what you describe.
Here is the link for you to have a look: https://jsonp.java.net/
Could you post also an example of the JSON you'll like to parse?

Use Jackson for parsing JSON. It will make your life easier.
This will help you.
http://www.tutorialspoint.com/jackson/jackson_first_application.htm
Thanks

Related

Json-path json extractor that consist from nodes with fullstop in their names

I use com.jayway.jsonpath:json-path for quick accessing and modifying nodes.
Example of pattern is: "$.dnode.meta" helps to manage data on meta node level that located inside of dnode. Example of json is:
{
"dnode": {
"meta": "some value"
}
}
I faced with json like:
{
"dnode.meta": "some value"
}
And I need help to construct path where node name contains full stop.
I tried next examples but they do not work for me:
"$.[dnode.meta]"
"$.\'dnode.meta\'"
"$.\"dnode.meta\""
"$.dnode\.meta"
...
is any ideas how to construct path with json node that includes .?
You can try with "$['dnode.meta']"

How to fetch sub json from request parameter

I just decided to send below json data from client to server. Then i found all my previous request were not of type json. And i am unable to send json. Below is the json i want to send in data of jquery ajax.
data:{
id:"10",
sampleArr:[
{ id:"hello","sample":"hello"},
{ id:"hello1","sample":"hello1"}
]
}
and at server i get below parameters
id=10
group[0][id]=hello
group[0][sample]=hello
group[1][id]=hello1
group[1][sample]=hello1
so i am confused how to fetch all groups
One problem is that what you are sending is not valid JSON.
{ "data" : {
"id" : "10",
"sampleArr": [
{ "id" : "hello", "sample" : "hello"},
{ "id" : "hello1", "sample" : "hello1"}
]
}
}
Notice that all attribute names must be quoted, and the top-level JSON object must have curly brackets around it.
If that doesn't help, you need to explain how your servlet is receiving and parsing the JSON.
#BigMike, Thanks i am able to fetch complete json and play around with it. Was unable to send JSON even setting content type application/json. But Still checking why, but working as temperary fix (Might be that i am not using Rest API)

Is there a better/easier way to update a json file in Java?

Say, I have a json file like below:
[{
"obj1_key1":"aa",
"obj1_array":[{"e1":"11"},{"e2":"22"}]
},
{
"obj2_key1":"cc",
"obj2_key2":"dd"
}]
Now I want update the file into something like below:
[{
"obj1_key1":"aa",
"obj1_array":[{"e1":"11"},{"e2":"22"},{"e3":"333"}]
},
{
"obj2_key1":"cc",
"obj2_key2":"dd"
}]
I tried using ObjectMapper to parse the file like
JsonNode jsonFile = new ObjectMapper().readTree(new File("file.json");
however then I need to find the obj1_array and append a json object, then write the json object back to the file. And I don't think the way I load the json file as a JsonNode is a easy way because I should convert it between Json/JsonArray back and forth. So I'm wondering is there a simpler way to make this work? Really appreciate that.
if it is just a one-off case, you can use your preferred mechanism, but if it is going to used often, I would prefer
Convert the JSON to a POJO ( using some parser eg Jackson )
Update the requisite fields
Return the Json object.

Getting the response from a json response to Google Maps API in Java

I need to develop a program where I have to calculate driving distance and time. Getting the request to the Google Maps API works, and I get a really big response. Now I want to extract the data I need from this response. The response is in json, and the program I make is made in Java. I have no idea to do this. I tried to just put the whole response in a string, and then search the data I need, like "distance" and extract a few characters that come after that. Problem with this is, if something changes in front of it, it's all going to be at different indexes in the huge string.
A response is like this (only a bit of the big thing)
"copyrights" : "Kaartgegevens ©2017 GeoBasis-DE/BKG (©2009), Google",
"legs" : [
{
"distance" : {
"text" : "229 km",
"value" : 229411
},
"duration" : {
"text" : "2 uur 20 min.",
"value" : 8417
},
How do I easily extract the value of both "distance" and "duration"?
Basically what you want to do is serializing and deserializing the JSON to a proper Java object. You can do this by using Frameworks like Jackson or GSON to convert the JSON-String to POJOs.
This might help you as well:
How to serialize and deserialize a JSON object from Google geocode using Java
Converting JSON to Java
http://thegeekyland.blogspot.de/2015/11/serializing-and-deserializing-json-from.html
And I'm pretty sure Google has it's own API that maps the JSON internally, although I haven't used it yet. Here you might find more information:
https://developers.google.com/maps/web-services/?hl=de
Hope this helps!

How to append object to a json file with JSR 353 (Java API for JSON Processing)

Using JSR-353 (https://jsonp.java.net/index.html)
I would like to open a json file and append some object in the root array, eg :
[{"foo":"bar"}]
I would like with a code about like this :
try(JsonGenerator writer = Json.createGenerator(new FileOutputStream(this.file))){
writer.writeStartObject().write("hello", "world").writeEnd();
} catch (IOException e) {
e.printStackTrace();
}
And obtain in the end :
[
{"foo":"bar"},
{"hello":"world"}
]
Note : I don't want to have to load the full json in-memory to append my data.
Note : I don't want to have to load the full json in-memory to append
my data.
Basically, you can't. You would have to parse the full data structure, so that your write(..) would know where to write. Otherwise, it's just appending somewhere and that might break the JSON format.
So read the JSON from the file, generate a JsonArray from it. Create a new JsonObject from your values. Add it to the array. Then write the full array.
You can't simply "append". In the general case you must read in the JSON, modify the tree-structured memory image, then "serialize" it back to linear JSON.
In very simple cases such as the above you could in theory seek to the end, backspace over the closing ], then write out ,, the second object, and a new closing ], but it's not a general solution to updating JSON.

Categories