Deserializing Json Object in java

23,971

Solution 1

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

Solution 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?

Share:
23,971
exSnake
Author by

exSnake

BY DAY: Working into a industry of Slot Machines as software developer, coordinating and scheduling worker and create/managing database. BY AFTERNOON: I'm a students of information technology Engineering at University of Salerno BY NIGHT: Jumping through community for give my help, i have a lot of hobbies and thing to do, but the time isn't enough for get all accomplished. FOR FUN: I love to code, create new application and actually (2016) i'm learning Java-PHP language.

Updated on July 09, 2022

Comments

  • exSnake
    exSnake almost 2 years

    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.