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.

1
  • Have you tried with Google GSON? Commented May 25, 2016 at 12:46

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

2 Comments

I did that, and finally came to the solution. thanks! This video too helped me alot: youtube.com/watch?v=PARyHhklbgc
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?

Comments

-3

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 Comment

It's a json created by javascript, but i need to deserialize it using java

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.