Deserializing Json Object in java [duplicate]

I cannot figure it out, I should deserialize a json object of this type:

{ "value":integer", "total":1", "records":138", "rows":[ { "value1":6, "value2":true, "bool":true, "floatNumber":140.41", "floatNumber2":28.7", "floatNumber3":140.41", "cssClassName":""", "date":"19/03/2022"", "UTCdate":"2016-03-22T00:00:00+0000"", "UTCdate2":"2016-03-24T20:45:25+0000" }, { "value1":6, "value2":true, "bool":true, "floatNumber":140.41", "floatNumber2":28.7", "floatNumber3":140.41", "cssClassName":""", "date":"19/03/2022"", "UTCdate":"2016-03-22T00:00:00+0000"", "UTCdate2":"2016-03-24T20:45:25+0000"} ]}

but I do not know how to do. I wish that this item was added to my class, pointing to what value to assign the corresponding property.

I tried to use Flexjson library but didn't saw any function that will let me what i want to do.

Where to start?

PS: I never serialized an object to JSON, so I do not know how it works.

1

3 Answers

You can go through this tutorial. Hope it will help you.

  1. How to convert Java object to / from JSON (Jackson)
2

That's json. You need to parse it using api.

For example

{'profiles': [{'name':'john', 'age': 44}, {'name':'Alex','age':11}]}

you will have to do something of this effect:

 JSONObject myjson = new JSONObject(the_json); JSONArray the_json_array = myjson.getJSONArray("profiles");

this returns the array object.

Then iterating will be as follows:

int size = the_json_array.length();
ArrayList<JSONObject> arrays = new ArrayList<JSONObject>();
for (int i = 0; i < size; i++) { JSONObject another_json_object = the_json_array.getJSONObject(i); //Blah blah blah... arrays.add(another_json_object);
}
//Finally
JSONObject[] jsons = new JSONObject[arrays.size()]; arrays.toArray(jsons);

Example code is taken from How to parse a JSON and turn its values into an Array?

Java or Javascript? You do know that these are 2 completely different languages?

In Javascript you do it like this:

// object to string:
JSON.stringify(object);
// string to object
JSON.parse(object);
1

You Might Also Like