-1

My JSON string.

{
    "RateCardType": [{
            "rate_id": 32,
            "applianceId": 59,
            "categoryId": 33,
            "install_Price": 599,
            "uninstall_Price": 0,
            "gasRefill_Price": 0,
            "repair_Price": 249,
            "basicClean_Price": 0,
            "deepClean_Price": 449,
            "demo_Price": 500
        },
        {
            "rate_id": 33,
            "applianceId": 59,
            "categoryId": 34,
            "install_Price": 799,
            "uninstall_Price": 0,
            "gasRefill_Price": 0,
            "repair_Price": 349,
            "basicClean_Price": 0,
            "deepClean_Price": 799,
            "demo_Price": 500
        }
    ]
}

MyRateCard.java

package com.example.demo;

import javax.persistence.Column;

public class MyRateCard {

    @Column(name = "rate_id")
    int rate_id;

    public void setRate_id(int rate_id) {
        this.rate_id=rate_id;
    }

    public int getRate_id() {
        return rate_id;
    }

    @Column(name = "applianceId")
    int applianceId;

    public void setApplianceId(int applianceId {
        this.applianceId=applianceId;
    }

    public int getApplianceId() {
        return applianceId;
    }

    @Column(name = "categoryId")
    int categoryId;

    public void setCategoryId(int categoryId) {
        this.categoryId=categoryId;
    }

    public int getCategoryId() {
        return categoryId;
    }

    @Column(name = "install_Price")
    int install_Price;

    public void setInstall_Price(int install_Price {
        this.install_Price=install_Price;
    }

    public int getInstall_Price() {
        return install_Price;
    }

    @Column(name = "uninstall_Price")
    int uninstall_Price;

    public void setUninstall_Price(int uninstall_Price) {
        this.uninstall_Price=uninstall_Price;
    }

    public int getUninstall_Price() {
        return uninstall_Price;
    }

    @Column(name = "gasRefill_Price")
    int gasRefill_Price;

    public void setGasRefill_Price(int gasRefill_Price) {
        this.gasRefill_Price=gasRefill_Price;
    }

    public int getGasRefill_Price() {
        return gasRefill_Price;
    }

    @Column(name = "repair_Price")
    int repair_Price;

    public void setRepair_Price(int repair_Price) {
        this.repair_Price=repair_Price;
    }

    public int getRepair_Price() {
        return repair_Price;
    }

    @Column(name = "basicClean_Price")
    int basicClean_Price;

    public void setBasicClean_Price(int basicClean_Price) {
        this.basicClean_Price=basicClean_Price;
    }

    public int getBasicClean_Price() {
        return basicClean_Price;
    }

    @Column(name = "deepClean_Price")
    int deepClean_Price;

    public void setDeepClean_Price(int deepClean_price) {
        this.deepClean_Price=deepClean_price;
    }

    public int getDeepClean_Price() {
        return deepClean_Price;
    }

    @Column(name = "demo_Price")
    int demo_Price;

    public void setDemo_Price(int demo_Price) {
        this.demo_Price=demo_Price;
    }

    public int getDemo_Price() {
        return demo_Price;
    }

}

I have created a model class MyRateCard.java with all getters and setters. I want to create a object for MyRateCard with first object in JSON string(say rate_id:32).

MyRateCard ratecard = new Gson().fromJson(response.toString(), MyRateCard.class);

But not working. Can someone help me to parse this?

Avijit Karmakar
  • 8,890
  • 6
  • 44
  • 59
blb007
  • 15
  • 8

3 Answers3

1
{
    "RateCardType": [{
            "rate_id": 32,
            "applianceId": 59,
            "categoryId": 33,
            "install_Price": 599,
            "uninstall_Price": 0,
            "gasRefill_Price": 0,
            "repair_Price": 249,
            "basicClean_Price": 0,
            "deepClean_Price": 449,
            "demo_Price": 500
        },
        {
            "rate_id": 33,
            "applianceId": 59,
            "categoryId": 34,
            "install_Price": 799,
            "uninstall_Price": 0,
            "gasRefill_Price": 0,
            "repair_Price": 349,
            "basicClean_Price": 0,
            "deepClean_Price": 799,
            "demo_Price": 500
        }
    ]
}

This is your JSON. This is JSON is an Object which contains an array of RateCardType.

You have created the RateCardType class.

Now create a class which consists of List of MyRateCard class.

class ListRateCard {
    List<MyRateCard> RateCardType;

    // write getter and setter
}

Now, write the below code:

ListRateCard ratecards = new Gson().fromJson(response.toString(), ListRateCard.class);

Fetch rateId by the below code:

ratecards.getRateCardType().get(0).getRate_id();
Avijit Karmakar
  • 8,890
  • 6
  • 44
  • 59
  • I created a inner class "class ListRateCard{List RateCardType; //getter and setter for every item }This line raising exceptions of type casting-- List ratecards = new Gson().fromJson(response.toString(), ListRateCard.class); – blb007 Jul 23 '17 at 05:54
  • Don't create an inner class. Create a normal java class named ListRateCard – Avijit Karmakar Jul 23 '17 at 05:55
  • Add the exception here. – Avijit Karmakar Jul 23 '17 at 06:04
  • 1)cast to java.util.List 2)change variable ratecards type to 'com.package.ListRateCard 3)Make ListRateCard implement java.util.List 4)Migrate ratecards type to com.package.ListRateCard – blb007 Jul 23 '17 at 06:06
  • Sorry, now check that line in the answer. I am written a wrong there. Now check the updated answer. – Avijit Karmakar Jul 23 '17 at 06:06
  • Exceptions gone but still 'ratecards.getRate_id()' gives 0 – blb007 Jul 23 '17 at 06:12
  • in ListRateCard.java I added all getters and setters. Now after this ListRateCard ratecards = new Gson().fromJson(response.toString(), ListRateCard.class); Log.i("blb demo",""+ratecards.getRate_id()); – blb007 Jul 23 '17 at 06:19
  • Your code is wrong. I don't know, how are you getting getRate_id method in ratecards object. I think you are messed up with those two classes. – Avijit Karmakar Jul 23 '17 at 06:20
1

These are 2 files.The MyPojo class is a holder for your actual data.In your json as you can the outer {} signifies an object,this object contains just 1 key called RateCardType.Hence the outer class called MyPojo.

Now the key RateCardType contains a list of objects as shown by the [] brackets,hence the List<RateCardType> .The rest is just data contained within the RateCardType class which you had got initially.

public class MyPojo
{
private List<RateCardType> RateCardType;

public List<RateCardType> getRateCardType ()
{
    return RateCardType;
}

public void setRateCardType (List<RateCardType> RateCardType)
{
    this.RateCardType = RateCardType;
}
}

public class RateCardType
{
private String repair_Price;

private String basicClean_Price;

private String uninstall_Price;

private String categoryId;

private String install_Price;

private String rate_id;

private String gasRefill_Price;

private String demo_Price;

private String deepClean_Price;

private String applianceId;

public String getRepair_Price ()
{
    return repair_Price;
}

public void setRepair_Price (String repair_Price)
{
    this.repair_Price = repair_Price;
}

public String getBasicClean_Price ()
{
    return basicClean_Price;
}

public void setBasicClean_Price (String basicClean_Price)
{
    this.basicClean_Price = basicClean_Price;
}

public String getUninstall_Price ()
{
    return uninstall_Price;
}

public void setUninstall_Price (String uninstall_Price)
{
    this.uninstall_Price = uninstall_Price;
}

public String getCategoryId ()
{
    return categoryId;
}

public void setCategoryId (String categoryId)
{
    this.categoryId = categoryId;
}

public String getInstall_Price ()
{
    return install_Price;
}

public void setInstall_Price (String install_Price)
{
    this.install_Price = install_Price;
}

public String getRate_id ()
{
    return rate_id;
}

public void setRate_id (String rate_id)
{
    this.rate_id = rate_id;
}

public String getGasRefill_Price ()
{
    return gasRefill_Price;
}

public void setGasRefill_Price (String gasRefill_Price)
{
    this.gasRefill_Price = gasRefill_Price;
}

public String getDemo_Price ()
{
    return demo_Price;
}

public void setDemo_Price (String demo_Price)
{
    this.demo_Price = demo_Price;
}

public String getDeepClean_Price ()
{
    return deepClean_Price;
}

public void setDeepClean_Price (String deepClean_Price)
{
    this.deepClean_Price = deepClean_Price;
}

public String getApplianceId ()
{
    return applianceId;
}

public void setApplianceId (String applianceId)
{
    this.applianceId = applianceId;
}
}

In order to use it

MyPojo holder= new Gson().fromJson(response.toString(), MyPojo.class);
List<RateCardType> list=holder.getRateCardType();
for(int i=0;i<list.size();i++)
{
 list.get(i).getBasicClean_Price();
 ....
}
Jude Fernandes
  • 7,437
  • 11
  • 53
  • 90
0

you can access any json level by tree hierachy

MyRateCard ratecard = new Gson().fromJson(response.toString(), MyRateCard.class);
String rateid=ratecard.rate_id;
K-Square
  • 63
  • 5