What kind of json starts with length - java

I am getting a json array from server like below
69[0,{"dabcdefghij":{},"abcdefg":"20","abcdefghijklmn":"10, AB-11111"}]
I know the data inside [ ] is json. But the server is also sending the length of the json.
Right now i am finding the first occurence of [ and parsing the json.
Is it the right way? I am using gson. Is there a better method to parse this?

69[0,{"dabcdefghij":{},"abcdefg":"20","abcdefghijklmn":"10, AB-11111"}] is not valid JSON according to json.org as it's not object nor an array.
JSON is built on two structures:
A collection of name/value pairs. In various languages, this is
realized as an object, record, struct, dictionary, hash table, keyed
list, or associative array.
An ordered list of values. In most languages, this is realized as an
array, vector, list, or sequence.

Related

JSON format for a Vector of numbers

I'd like to populate a Java double vector, double dNumber[], from a JSON String. In Java I just have one variable dNumber.
Is there any way I can use one variable in JSON with the list of values. I don't want to create a separate JSON name for each item in the array.
Thanks.
Al
Yes.
A JSON can contain an array of values. The syntax is
{
dNumbers: [ 12.5, 6.8, 3.1415],
[...]
}
More on JSON here

How to parse a JSON map in Java or Kotlin where the map has a key

I'm sending a Map from a server to a Android app as a JSON file. Previously I was sending Arrays, Strings, Booleans, and Longs which were decoded by null checks and
json.getJSONArray(key)
or similar, where json is a JSONObject and key is a String.
.getJSONMap(key) does not exist and other similar questions do not provide answers that include the case where the JSON has other data and needs a key to specify where the map is.
What can I use to parse the JSON to a map given a key?
You want json.getJSONObject(key)

How to get json as a string from a url?

I want to have an array with the value of json:
arr[ ] array = {"http://www.ip-api.com/json"};
when I print this array, how can I have json but not "http://www.ip-api.com/json" as a string?
Use a JSON generating library such as Jackson and have it serialize the data structure.
https://github.com/FasterXML/jackson
If you want to actually mean that you want to call the service at that URL then you need to use a suitable library for that...

JSONSmart parsing a whole array and getting each object?

An example JSON.
EDIT: I've put it on a pastebin because of how big the file is - http://pastebin.com/wdR2paBp
How would I get an Array of "objects", then iterate through it and get the name (i.e. "minecraft/sounds/dig/sand4.ogg") and the hash from each of these files?
My attempts:
FileReader fr = new FileReader(location.getAbsolutePath());
JSONArray iIndexes = (JSONArray) parser.parse(fr);
I've also tried making Objects a JSONObject then making it a JSONArray then using a for loop to get every object, but I get a NPE or a ClassCast Exception (for the atttempt before this one).
What you have there is a JSON object, not a JSON array. Conceptually, you can treat it as a set of name/value pairs, but according to the JSON specification, the NV pairs are not ordered; i.e. it is a set, not a list.
There is no standard way to turn that JSONObject into a JSONArray. Casting won't work, and the JSONObject doesn't have a method to do the conversion. And certainly, there is no way using JSONSmart to preserve the apparent order of the NV pairs in your source file / string. (Which is a good thing, IMO, because the order shouldn't mean anything.)
If you want to iterate the NV pairs, the best way to do it is to use the entrySet method to get the JSONObject's entries as a Set ... and then iterate the set. (The JSONSmart version of JSONObject is a subclass of HashMap.)
Now if set of entries in "objects" is supposed to be ordered, then you have designed your JSON scheme incorrectly. You should be using a JSON array (using the [...] JSON syntax) and the elements need to be restructured as objects; e.g.
[ {
"name": "realms/lang/de_DE.lang",
"hash": "10a54fc66c8f479bb65c8d39c3b62265ac82e742",
"size": 8112
},
{
"name": "realms/lang/cy_GB.lang",
"hash": "14cfb2f24e7d91dbc22a2a0e3b880d9829320243",
"size": 7347
},
etcetera
]
When you parse that using JSONSmart you will get a JSONArray.

Java JSON object insertion order maintenance

I am using a Java JSON object to store some data. But when I printed it, I found that it stores the data randomly. For example, I stored data like this:
obj.put("key1","val1");
obj.put("key2","val2");
And when I printed it:
{"key2":"val2","key1":"val1"}'
I googled it and found that JSON objects are unordered sets of key value pair. So it doesn't store the order of data.
I need some help in storing data in a JSON object with their order.
Arrays are ordered so use an array of key-value objects [ {key1: val1}, {key2: val2} ]

Categories