Is there any out of box method in java (groovy) that converts the string like
String s = "[a:12,b:[a:b,c:d]]";
to a Map object with key value pairs.
Update: It is somehow similar to the question asked as Groovy: isn't there a stringToMap out of the box?(Groovy: isn't there a stringToMap out of the box?). The difference is here the keys are of string type which makes easier to parse, since i have retrieved the mentioned map by doing .toString() i am unable to parse by the answered methods because my string actually contains date strings which i need it back to date objects. So it was difficult to parse the whole string.
You can convert this string to JSON-like format, and simple parse it:
String s = "[a:12,b:[a:b,c:d]]";
def result = new JsonSlurper().setType(JsonParserType.LAX).parseText(s.replaceAll('\\[', '{').replaceAll('\\]', '}'))
I have a way that may work depending on your actual data. The problem with your sample data is that you supply unquoted strings b and d as part of the structure:
String s = "[a:12,b:[a:b,c:d]]";
If what you had was actually
String s = "[a:12,b:[a:'b',c:'d']]";
or
String s = "[a:12,b:[a:1,c:2]]";
Then you could just do this:
def map=Eval.me(s)
but as is, eval tries to resolve b and d as variables which aren't defined in the scope.
Related
I am getting a JSON in below format:
{
"A":"1",
"B":"2"
}
I have a field update and JSON at some point could be too long.
How can I change the JSON format to below pattern?
{"A":"1","B":"2"}
I am trying to store this minified JSON format on a field, so that char limit issue is resolved.
deserialize the string and serialize it again and then update the field.
Declare the JSON as string
Map<String,Object> parser = (Map<String,Object>)JSON.deserializeUntyped(yourstring);
yourstring = JSON.serialize(parser);
You can change Map accordingly. Required format will be present in string variable.
I have a case where I need to store a location of each key value of json, so that for each key, it automatically fetches the location and gives the value for it from json.
Here, I have a location of the key 'vehicle_id' inside json 'car' assigned to a variable like:
String location="jresp.getJSONArray('cars').getJSONObject(0).getString('vehicle_id')"
How do I make it as a variable in JAVA such that this location fetches the value of vehicle_id for me from JSON? I need it like:
String value=jresp.getJSONArray('cars').getJSONObject(0).getString('vehicle_id');
so that it gives me a value. I've searched in net, but couldn't find it anywhere. Please help me!
Instead of having the complete statement as a variable like -
String location="jresp.getJSONArray('cars').getJSONObject(0).getString('vehicle_id')"
You could have 3 different variables like -
String jsonArray = "cars";//You might need to do some string processing to get these
int objSeq = 0;
String key = "vehicle_id";
Then you can definitely use it in your Java statement -
String value=jresp.getJSONArray(jsonArray).getJSONObject(objSeq).getString(key);
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 am building an app which utilizes the Parse back-end. As part of my user object I want to directly use the FacebookID as my UserID, but I want to convert it to a BigInt instead of a string.
I used the following code, but when I check the data in Parse the value is slightly off.
"user" is a GraphUser object which I have declared above.
String iDString = user.getId();
BigInteger iD = new BigInteger(iDString);
currentUser.put("faceID", iD);
When I check the data in Parse I get the following:
Original ID in string format (* are used to cover digits):
************27645
BigInt ID:
************27644
Does anyone know why the last digit is 1 less than the digit above? May it have something to with the sign of the BigInt?
I don't know which is your problem due to the lack of information in your post.
This simple case works very well for me though:
String iDString = "99999999999997645";
BigInteger iD = new BigInteger(iDString);
System.out.println(iD.toString());
The printed number is exactly the same of the iDString "number".
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.