3

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.

exSnake
  • 682
  • 1
  • 8
  • 26

3 Answers3

5

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

  1. How to convert Java object to / from JSON (Jackson)
  2. https://dzone.com/articles/deserializing-json-java-object
SkyWalker
  • 28,384
  • 14
  • 74
  • 132
  • I did that, and finally came to the solution. thanks! This video too helped me alot: https://www.youtube.com/watch?v=PARyHhklbgc – exSnake May 29 '16 at 22:36
  • first link has been updated: https://mkyong.com/java/jackson-2-convert-java-object-to-from-json/ – theRiley Mar 13 '22 at 01:37
3

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?

Community
  • 1
  • 1
Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78
-1

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);
Azamantes
  • 1,435
  • 10
  • 15