I have a requirement where in I have to get all the key values of a json returned now I am getting the json as a string.
String test = obj.returnJSON();
I need to get the JSON key values as a list is there any predefined method or I have just to write my own logic.
Thanks
KD
Use the JSON in Java library http://json.org/java/ or the google-gson library https://code.google.com/p/google-gson/. Either of these will parse the javascript string into an object, and you can get the keys from there.
Using the Java org.json parser you could do it as
String test = obj.returnJSON();
JSONArray keys = new JSONObject(test).names();
System.out.println(keys.getString(0)); // key1
the google library, GSON, is WAY BETTER and very useful.
I had the same problem and found the answer here
Best regards!
Related
I am writing an application/class that will take in a template text file and a JSON value and return interpolated text back to the caller.
The format of the input template text file needs to be determined. For example: my name is ${fullName}
Example of the JSON:
{"fullName": "Elon Musk"}
Expected output:
"my name is Elon Musk"
I am looking for a widely used library/formats that can accomplish this.
What format should the template text file be?
What library would support the template text file format defined above and accept JSON values?
Its easy to build my own parser but there are many edge cases that needs to be taken care of and I do not want to reinvent the wheel.
For example, if we have a slightly complex JSON object with lists, nested values etc. then I will have to think about those as well and implement it.
I have always used org.json library. Found at http://www.json.org/.
It makes it really easy to go through JSON Objects.
For example if you want to make a new object:
JSONObject person = new JSONObject();
person.put("fullName", "Elon Musk");
person.put("phoneNumber", 3811111111);
The JSON Object would look like:
{
"fullName": "Elon Musk",
"phoneNumber": 3811111111
}
It's similar to retrieving from the Object
String name = person.getString("fullName");
You can read out the file with BufferedReader and parse it as you wish.
Hopefully I helped out. :)
This is how we do it.
Map inputMap = ["fullName": "Elon Musk"]
String finalText = StrSubstitutor.replace("my name is \${fullName}", inputMap)
You can try this:
https://github.com/alibaba/fastjson
Fastjson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Fastjson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.
I have a problem with converting values from Java Request into JSON String.
Im looking for good library, which convert my param keys and values into JSON.
So far I've written a class, which turns my map of params into JSON String. However, as we know, request params keys looks like this:
dto.author.name="Davos"&dto.author.age=47&dto.code="045f"
My class can convert that request params into JSON String which looks like this:
{'author':{'name':'Davos'},'author':{'age':47},'code':'045f'}
which is not quite good for Gson object, because 'author' object inside JSON object is beingrepeated for every 'author' request value, so Gson can't handle that and fill only one repeated object value.
My question is, is there any library, which gets Map of paramerers like
dto.author.name='Davos'
dto.author.age=47
dto.code='045f'
dto.books=[{'title': 'testTitle'}, {'title': 'secondTitle'}]
and can produce JSON String with nested objects based on 'bean' keys? I expect that final output will looks like that ('dto.' key fragment is being removed before execution):
{'author':{'name':'Davos','age':47},'code':'045f','books':[{'title': 'testTitle'},{'title': 'secondTitle'}]}
Any ideas guys? I'm revlolving aroud JSON-simple library, but for now I can't figure out, how it would handle 'bean' keys and convert it into nested JSON objects.
I'm relatively new to using JSON and all I really need to do read in a few key value pairs from a JSON file on the file system.
What I figured I would do is read in the file as a string and then parse it that way but it seems kind of redundant that way.
Here's what my file will be like:
{
"username" : "myname"
"domain" : "mydomain"
}
So essentially I need some help making an easy and efficient block of code to read in the key/value pairs. I've been trying to use GSON for the most part and haven't had much luck with examples I've found.
Thanks everyone
One other alternative is JSON.org, in which creating a JSON object from a JSON string requires only one line:
JSONObject jsonObject = new JSONObject(someJSONString);
When you need to access its value, use the functions that the JSONObject provides. For example,
String userName = jsonObject.getString("username");
String domainName = jsonObject.getString("mydomain");
Hi I have a json input file as follows,
{'Latitude':'20',
'coolness':2.0,
'altitude':39000,
'pilot':{'firstName':'Buzz',
'lastName':'Aldrin'},
'mission':'apollo 11'}
How to create a java object from the json input file.
Thanks
You can use the very simple GSON library, with the Gson#fromJson() method.
Here's an example: Converting JSON to Java
There are more than one APIs that can be used. The simplest one is JSONObject
Just do the following:
JSONObject o = new JSONObject(jsonString);
int alt = o.getInt("altitude");
....
there are getXXX methods for each type. It basically stores the object as a map. This is a slow API.
You may use Google's Gson, which is an elegant and better library -- slightly more work required than JSONObject. If you are really concerned about speed, use Jackson.
I have a string in an Android app that I am trying to convert into a JSONObject. The string looks like this (except longer and with actual values instead of the dummy values I entered here):
[[{"1":"a"}],[{"1a":"1a","1b":"1b"},{"2a":"2a","2b":"2b"}]]
I have entered this exact string into two online JSON validators, and both of them confirm it to be valid JSON data. So I would assume that the JSONObject constructor would be able to accept this string and convert it into a JSONObject. But when I try:
json = new JSONObject(result);
Where "result" is a String variable containing the string listed above, I get the following exception:
JSONException: A JSONObject text must begin with '{' at character 1 of [[{"1":"a"}],[{"1a":"1a","1b":"1b"},{"2a":"2a","2b":"2b"}]]
What's going on here? Is the JSONObject's parser broken?
You are trying to create a JSONObject, but what you are actually giving it is a JSONArray. Did you try creating a JSONArray instead?
Alternatively, you could wrap your array in an object so that you can create a JSONObject out of it.
I would suggest using the GSon library instead as it appears to be more full-featured.
In addition, it may be helpful to use this tool to test your data (your data is valid btw):