I am looking for suggestions on how best to send multimap values from java to javascript in json format. Besides using an object with a key field and an arraylist, are there any other options I could explore to send multimap values in json format for processing in javascript on the browser? thanks in advance
If you intend on processing all the values you might just want to send it as a list of pairs.
Otherwise then that map to lists is your friend.
Some code for clarity.
// list of pairs
[["key1", "val11"],
["key1", "val12"] .....]
// map to lists
{ "key1": ["val11", "val12"], ...... }
Related
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)
My Question seems very similar to this question, but what happens if the are duplicate values in the json file?
The duplicate values are found in the json file due to the file contents originating from postgres which allow to insert duplicate values in older JSON format files.
My input look like this.
{
"61":{"value":5,"to_value":5},
"58":{"r":0,"g":0,"b":255}, "58":{"r":165,"g":42,"b":42},"58:{"r":0,"g":255,"b":0},
"63":{"r":0,"g":0,"b":0},
"57":{"r":0,"g":0,"b":255},"57":{"r":0,"g":255,"b":0}
}
If you look carefully there are multiple values of "58" as keys. The main keys "61" and "58" are mappted to a nested map type with different keys.
Now to simplify what I want to achieve, my output of the above input json should look like this.
Approach or solution both equally appreciated in java only.
{
"61":[5,5],
"58": [{"r":0,"g":0,"b":255},{"r":165,"g":42,"b":42},{"r":0,"g":255,"b":0}],
"63":[{"r":0,"g":0,"b":0}],
"57":[{"r":0,"g":0,"b":255},{"r":0,"g":255,"b":0}]
}
A good tool for parsing JSON formats is this library: JSONObject
Here an example of usage in a previous SO question:
Parsing JSON which contains duplicate keys
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 have a socket server and Im sending information from client to server and back.
Sock servers will only send strings or byte arrays of information.
The information I need to transfer is java hashmap, but to send it, I convert it to a string.
activeUsers = new HashMap<Object, Object>();
socket.send(activeUsers.toString());
upon receiving this this string, I need to reconstruct it as a hashmap.
I have seen examples like this, however, I think this would not work for my case.
For instance:
an item in the hashmap can be while the next item is <string, hashmap> and the items in that hashmap could be a combinations of the two.
I'm used to python where I can say evaluate string as dictionary and it does so.
How can I achieve the same thing in java.
Convert the your HashMap to JSON using the technique given here:
How to convert hashmap to JSON object in Java
Then after transferring the string data, convert it back to HashMap using the Gson or Jackson library for 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.