-1

Given this JSON response I get from an website :

{
  "Items":
   [
    { "Name":"Apple", "Price":12.3, "Quantity":30 },
    { "Name":"Grape", "Price":3.21, "Quantity":60 }
   ],
   "Date":"21/11/2010"
}

How could i deserialize this JSON, splitting it in an array called Fruits, containing only name and quantity ? I don't care about date field or other fields like price.

My class should look like:

class Fruit{
   String name;
   String quantity;
}

And this is the array:

Fruit myfruits[] = new Fruit [this number depends on  JSON response I get]

How could I achive this ? I've tried to give my best explanation, if it is still not clear, feel free to ask.

P.S: btw, the real JSON response has many more fields

user3191545
  • 25
  • 1
  • 6
  • possible duplicate of [Gson: How to exclude specific fields from Serialization without annotations](http://stackoverflow.com/questions/4802887/gson-how-to-exclude-specific-fields-from-serialization-without-annotations) – Udy Nov 28 '14 at 15:46
  • wasn't the answer helpful ? – Udy Dec 08 '14 at 10:12

1 Answers1

0

You need to ignore the fields you don't wont.

Each serialization frameworks does it in different ways.

In some you can add annotations to you POJO, or set it with the serializer instance

Using Gson:

Gson ignore json field and deserialize

Using Jackson:

Ignoring new fields on JSON objects using Jackson

Community
  • 1
  • 1
Udy
  • 2,492
  • 4
  • 23
  • 33