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
Related
I am working on a calculator like assignment for my data structures class, and I have to input symbols into the array list defined as
ArrayList<ScalarSymbol> scalar;
and I have been trying to input values into it by doing this:
scalars.add(0,')');
and it tells me that the char type is not the correct syntax for the <ScalarSymbol> array. I don't know how to find out what types the array will take, is there bind I have to use in order to make it accept chars?
Just create an instance of the object you want to add:
ScalarSymbol s = new ScalarSymbol();
Then add it to the list:
scalar.add(s);
The array list only takes the type written in these:
<type>
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...
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.
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} ]
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.