【问题标题】:Parsing JSON String to Object将 JSON 字符串解析为对象
【发布时间】:2017-07-23 05:07:12
【问题描述】:

我的 JSON 字符串。

{
    "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;
    }

}

我创建了一个包含所有 getter 和 setter 的模型类 MyRateCard.java。我想用 JSON 字符串中的第一个对象为 MyRateCard 创建一个对象(比如 rate_id:32)。

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

但不工作。有人可以帮我解析一下吗?

【问题讨论】:

  • 附上MyRateCard类的代码。
  • this 有帮助吗?
  • MyRateCard 类列表中你使用过任何类吗?
  • 我是这个概念的新手,所以不知道该怎么做。 @AvijitKarmakar 我没有使用任何东西。

标签: java android json gson


【解决方案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
        }
    ]
}

这是你的 JSON。这是 JSON 是一个包含 RateCardType 数组的对象。

您已经创建了 RateCardType 类。

现在创建一个包含 MyRateCard 类列表的类。

class ListRateCard {
    List<MyRateCard> RateCardType;

    // write getter and setter
}

现在,编写以下代码:

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

通过以下代码获取rateId

ratecards.getRateCardType().get(0).getRate_id();

【讨论】:

  • 我创建了一个内部类 "class ListRateCard{List RateCardType; //getter and setter for each item }这行引发类型转换的异常-- List ratecards = new Gson( ).fromJson(response.toString(), ListRateCard.class);
  • 不要创建内部类。创建一个名为 ListRateCard 的普通 java 类
  • 在此处添加例外。
  • 1) 转换为 java.util.List 2) 将变量 ratecards 类型更改为 'com.package.ListRateCard 3) 使 ListRateCard 实现 java.util.List 4) 迁移ratecards 类型为 com.package.ListRateCard
  • 对不起,现在检查答案中的那一行。我在那里写错了。现在检查更新的答案。
【解决方案2】:

这些是 2 个文件。MyPojo 类是您的实际数据的持有者。在您的 json 中,外部 {} 表示一个对象,该对象仅包含一个名为 的键RateCardType。因此外部类称为 MyPojo

现在 RateCardType 键包含一个对象列表,如 [] 括号所示,因此是 List&lt;RateCardType&gt; 。其余的只是 RateCardType 类中包含的数据你最初得到的。

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;
}
}

为了使用它

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();
 ....
}

【讨论】:

  • 你能详细解释一下吗?这是一个文件还是两个文件?
  • @blb007 我编辑了答案,让您了解正在发生的事情
  • 我现在可以使用这条线吗? MyRateCard ratecard = new Gson().fromJson(response.toString(), MyRateCard.class);
【解决方案3】:

您可以通过树层次结构访问任何 json 级别

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

【讨论】:

  • Log.i("blb",""+ratecard.getRate_id());打印 0
猜你喜欢
  • 2013-03-12
  • 2019-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-14
  • 1970-01-01
  • 2013-05-12
  • 2020-05-18
相关资源
最近更新 更多