Java JSON object insertion order maintenance - java

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} ]

Related

Reading a SSTable in Java

Sorted Strings Table is a file of key/value string pairs, sorted by keys
But it is not clear what fields SSTable entity (class) should have
Should we store all values?
It is clear that we save the shift for each key, but I still cannot fully understand how to store all this
After reading a good article, I still don't fully understand: https://www.igvita.com/2012/02/06/sstable-and-log-structured-storage-leveldb/

How to read/update values stored inside a map which is then inside of an array in Cloud Firestore?

To begin with I'm a newbie to Firestore and Android programming.
I have a collection which stores documents, each document contains an array of semesters, inside the semester array there will be an array of maps representing each semester. Inside the individual semester maps, there will be a number field identifying the semester and also another array called Students where it will contain an array of maps where each map will represent each student and store three different fields: ID of the student, DaysPresent, Total.
I'm trying to find a way to read the data for each individual student represented by ID in such a way where the semester number is taken into account.
For example, if the user selects Semester 1, it will go inside the array Semester and then load up the values from the first array which represents the Semester 1, and it will then display all the maps that represent each student where the days present and the total can be modified and read by the user.
One crucial key point is that the total number will always be the same for every individual map which means every individual student.
Edit: Mar 26th, 2020:
Actually it's possible using the solution in the following article:
How to map an array of objects from Cloud Firestore to a List of objects?
If you intend to get a List<Semester> or a List<Student> when querying your Cloud Firestore database, please note that this is currently not possible. Even if those objects are of type Semester and Student, there is no way you can map them into a List of Semester/Student objects. To solve this, you need to write code to iterate those objects so you can convert them from a List<Map> to a List<Semester>and List<Student>. So you need to apply your own logic for that.
Edit:
The type of object that is returned when calling:
document.getData();
Is a Map<String Object>, so you need to iterate through the map according to your logic and get the data as needed.

Java Data Structures with Redis

In my code I have declared this data structure below:
LinkedHashMap<String, TreeMap<Integer, LinkedHashMap<String, String>>> GroupsOfaSignature = new LinkedHashMap<String, TreeMap<Integer, LinkedHashMap<String, String>>>();
I want to save it in redis which has its own types of data structures(Strings,hash,...) .In the redis documentation they said that we can store hashmap Where the key and the values are Strings so my question is if i can store a hashmap where the keys or values are not strings,for example a linkedhashMap like the example above
You need to convert them as strings. You can either use some Json libraries like gson to give you the equivalent Json string or you can use message pack to achieve the same.
http://msgpack.org/index.html
Hope this helps
You have to convert them to String though you can also store bitmaps . I would say when you have to store such a a representation either prefix key with some unique identifier for all the internal maps or use Json representation
You can also think of a Redis hash as a JSON object (with non-nestable objects).
Key: "usernameToUidMapping:a"
Value: "alpha" => "1"
"adam" => "312"
"acrobat" => "333"
"aromatic" => "664"
Refer below link
Redis Data Types

What kind of json starts with length

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.

iterate over a list and arrange data

I am getting a list of data in the form of List object from database which contains data as
field_id value dt_updated
1 3 jun-6
2 5 jun-5
1 2 jun-3
3 5 jun-3
i want iterate over this list and group data for for fields as per there old and new values.For example:i want create a json like:
"field_1":
[
oldvalue"2"
new value:"3"
],
field_2 :
[ oldvalue:"5"],
i want to iterate over the list and get the old and new values for that field and put in some object.How to perform this? In some cases field will have only old values.
I don't know if entries from database are ordered by dt_update but I assume it's not guaranteed.
Just put your entries from db to Map(id->DBUpdate) where DBUpdate is some simple value object with value and updateDate fields into some MultiMap (Available e.g. in Google Guava library). After iterating over all objects from DB sort elementes for each id by updateDate (simply by implementing Comparable interface in DBUpdate) and construct appropriate String (if there is only one value for given id in MultiMap there will be only oldValue, otherwise use first element for new value and second element in MultiMap as oldValue)

Categories