I am trying to convert string like this:
{"Shops":[
{"city":"Riga","shops":[{"a":"some info here","b":"...","c":"..."},{"a":"some info here","b":"...","c":"..."}]},{"city":"Liepaja","shops":[{"a":"info here","b":"info....","c":"..."}]
]}
to 2d array, like
shops[0][0]=>{"a":"some info here","b":"...","c":"..."}
shops[1][0]=>{"a":"info here","b":"info....","c":"..."}
Is it possible? Is there some easy way to do that?
I've searched, tried, but I still don't know how to do that.
I'm new in java.
That is a JSON string. There are a number of libraries that will do this for you.
JSON in Java
GSON
That looks like JSON data, and you should treat it as such.
Try a JSON parsing library for Java. I like GSON for its simplicity. Take a look at the Gson.fromJson() set of methods.
The type of data you have posted is JSON encoded. you could use a json encoder and decoder to do this job easily.
Related
I'm trying to make a Minecraft function compiler in Java, and data tags in Minecraft are very similar to JSON objects, but not quite the same. In general, they follow the JSON format, so I've been using JSONObjects, but for some reason, it wants me to have an 'f' after each float to show that they're floats, contrary to JSON standard of just writing the number.
Is it possible to export a JSON string with floats in that format without re-writing the whole exporter? If not, what's the best way to do this without writing spaghetti code? I will not need to re-parse it in Java.
I need to convert all the number values to string in my JSON file to overcome a NumberFormatException due to exceeding the Long.Max_Value limit. I am using json-simple JSONParser and it throws an exception. What's the best way to convert them in Java?
At the moment, I can't even parse the file completely due this exception.
look this examples https://sites.google.com/site/gson/gson-user-guide . Google gson library is probably what you need:
can convert object to json
can convert json to object
and many other functions
Is there a way to convert a Java object to a string like below?
Note that all the filed names should be escaped, and "\n" is used as to separate records.
{
"content":"{\"field1\":123, \"field2\":1, \"field3\":0, \"field4\":{\"sub1\":\"abc\", \"sub2\":\"xyz\"}}\n
{\"field1\":234, \"field2\":9, \"field3\":1, \"field4\":{\"sub1\":\"xyz\", \"sub2\":\"abc\"}}"
}
Thanks,
You can use GSON for that task.
Gson 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. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.
If you need to have a better readable representation, you may use the pretty-print feature.
Gson gson = new GsonBuilder().setPrettyPrinting().create();
To realize something like your example, you could in a first step serialize your content class, put the resulting string as a property in another class and serialize that one again.
That way GSON takes care of the escaping of ".
If you collect your strings in an array and use the pretty print option shown above, you get something similar to your line-break requirement, but not quite the exact same.
The result of the process described above may look like the following:
{
"content": [
"{\"field1\":123, \"field2\":1, \"field3\":0, \"field4\":{\"sub1\":\"abc\", \"sub2\":\"xyz\"}}",
"{\"field1\":234, \"field2\":9, \"field3\":1, \"field4\":{\"sub1\":\"xyz\", \"sub2\":\"abc\"}}"
]
}
Another alternative is to use the Json-lib library http://json-lib.sourceforge.net
String jsonStrData = " ....... ";
JSONObject jsonObj = JSONObject.fromObject(jsonStrData);
System.out.println(jsonObj);
Like GSON, json-lib handles escaping for you, more info on how to use it here http://json-lib.sourceforge.net/usage.html
I have a json data that I need to be parse in java The data is in the form
["string1","string2","string3",...]
Any idea how I can do that?
You can use JacksonJSON. For a good tutorial, have a look here.
You can use GSON api and use the code as below
Type type = new TypeToken<Collection<String>>(){}.getType();
List<String> results = new Gson().fromJson(json, type);
http://code.google.com/p/google-gson/
It depends a bit on how complete you want toe JSON parsing to be. If the above example is representative of all you expect, you might as well do some good old string parsing with indexOf and split.
If you want more complete JSON parsing, I'd suggest looking at the official json.org site and their Java page
I am using JSON-lib library for java http://json-lib.sourceforge.net
I just want to add simple string which can look like JSON (but i do not want library to automatically figure out that it might be json and just to treat it as string). Looking into source of library I can't find the way to do it without ugly hacks.
example:
JSONObject object = new JSONObject();
String chatMessageFromUser = "{\"dont\":\"treat it as json\"}";
object.put("myString", chatMessageFromUser);
object.toString() will give us {"myString":{"dont":"treat it as json"}}
and i want just to have {"myString":"{\"dont\":\"treat it as json\"}"}
How to achieve it without modifying source code ? I am using this piece of code as transport for chat messages from users - so it works OK for normal chat messages, but when user will enter JSON format as message it will break it because of default behavior of JSON-lib described here.
If I understand question correctly, I think json-lib is unique in its assumption of a String being passed needing to be parsed. Other libs typically treat it as String to include (with escaping of double-quotes and backslashes as necessary), i.e. work as you would expect.
So you may want to consider other libraries: I would recommend Jackson, Gson also works.
json-simple offers a JSONObject.escape() method.