I saved a json into the database as a string like this:
"[_district:_1_2_5village, _name:_1_1_2id_inter, _gender:_1_3_5sex]"
Now i want to convert it back to a Json Object so as to pick the key and value eg _district is the key and _1_2_5village is the value. Any help on how i can achieve this. Thanks
I tried to convert the string back into JSON by parsing but that dint work for me.
It doesn't work because that's not a JSON format, a JSON is a way of mapping objects and uses key value syntax like so:
{"key": "value"}
and an array would look like this:
[{"key": "value"},{"key": "value"}]
You'll need to make a custom parser for your syntax
Here's the json specification:
https://www.json.org/json-en.html
Related
I have a server response that has this format:
{"bankAccounts":[
{"id" :"wfasfsd",
"balance":{"amount":90, "currency":"GBP"}
}
]
I cant find out how to parse through it efficiently
For a similar response with of simpler format this post helped me (Extract JSONObject and iterate through HashMap field)
The picture shows exactly how the server response looks like. I need to extract this into JSON and in the end, I need to have a HashMap with keys id, amount, currency
You can use json-simple library to read and write json objects.
Here you can see how to use it.
Am using http://jsoniter.com/ java lib to parse json , it seems with this lib , it only possible to iterate over through the JSON and did not provide any api to get value for specific key like we have in org.json like below
jsonObject.get("some_key")
so do we have such type of getter methods in http://jsoniter.com/ also , can any one please help me in that .
This is probably already too late, but you can do it like this:
String jsonString = "{'a':1,'b':'text'}".replaceAll("'", "\"");
Any jsonObject = JsonIterator.deserialize(jsonString);
long number = jsonObject.get("a").toLong();
String text = jsonObject.get("b").toString();
I'm trying to convert an xml to json data using the java-json.jar . The conversion is done. but have issues with the resulting JSON data like
An Integer is expected but the data is converted as String.
it would not create a list if there was only one child element ( even when it should be a list as per the JSON schema ).
Is there any way to convert XML into JSON based on the JSON schema in Java?
We have a requirement of parsing xml data in java.
The xml data looks like this:
xml_data
1> The first question here is how to parse such data.I have tried the code suggested in this link :
xmlparse with Node
and have partial success. The only doubt here is how can i can to reach "" tag.
2>The second question is if the data is parsed how can we can store the values in single object,so that we can get length and values of "CRAWL", "Test", "Check"(Tables) separately(from single object)?
Please help me as how can we achieve this?
Abhi
I am now using java method com.mongodb.util.JSON.parse(param) to convert my json string to a DBObject.I find that, in most cases , for example , when the key in the json string is simple type like string or int ,it can be used by both JSON.parse() method and terminal.
For example:
json string:{'times':8}
Both JSON.parse({'times':8}) and db.collection.find({'times':8}) in terminal can work correctly.
But when I do a query on ISODate or _id,things become different:
json string 1 :{'createDate':ISODate('2013-10-21T06:39:16.692Z')}
json strign 2: {'createDate':{'$date':'2013-10-21T06:39:16.692Z'}}
json string 1 can work correctly in terminal but cannot be parsed by JSON.parse() method.
On the contrary , json string 2 can be parsed by JSON.parse() method but cannot be used in terminal.
The same thing happened in _id.
json string 3:{'_id':ObjectId('1231daf213432414321431')}
json string 4:{'_id':{'$oid':'fadf234234sdfadfasdfa12'}}
json string 3 can work correctly in terminal but cannot be parsed by JSON.parse() method.
On the contrary , json string 4 can be parsed by JSON.parse() method but cannot be used in terminal.
I don't know the reason of these differences.
The constructors you are using in the Mongo Shell are JavaScript code that is obviously not part of strict JSON that you want to parse in your Java application. The point to be criticized here is rather that the shell does not support something like '$date' (as I have just tested myself as well).
This is for example discussed here.