2017-07-23 169 views
-1

我的JSON字符串。解析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; 
    } 

} 

我創建了一個模型類MyRateCard.java所有getter和setter。我想用JSON字符串中的第一個對象爲MyRateCard創建一個對象(比如rate_id:32)。

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

但不工作。有人可以幫我解析這個嗎?

+2

附加'MyRateCard'類的代碼。 – fluffyBatman

+0

[this](https://stackoverflow.com/a/20523955/2815219)有幫助嗎? –

+0

你有沒有使用任何類的MyRateCard類的列表? –

回答

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水平。這是JSON是一個包含RateCardType數組的對象。

您已創建RateCardType類。

現在創建一個由MyRateCard類List組成的類。

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(); 
+0

我創建了一個內部類「類ListRateCard {列表 RateCardType; //獲取和設置爲每個項目 }這一行提高類型casting--列表 ratecards =新GSON()fromJson(響應的異常。 toString(),ListRateCard。類); – blb007

+0

不要創建一個內部類。創建一個名爲ListRateCard的普通Java類 –

+0

不變更同樣例外 – blb007

0

您可以通過樹層次結構

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

+0

Log.i(「blb」,「」+ ratecard.getRate_id());打印0 – blb007

1

這些是2個files.The MyPojo類是持有人的實際數據。在您的json中,外部{}表示一個對象,該對象只包含一個名爲的鍵。RateCardType。因此外部類別稱爲MyPojo

現在關鍵RateCardType包含由括號[]表示,對​​象列表因此List<RateCardType>。剩下的只是包含在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(); 
.... 
} 
+0

你能詳細解釋一下嗎?這是單個文件還是兩個文件? – blb007

+1

@ blb007我編輯了答案,讓你知道最新情況 –

+0

現在可以使用這條線了。 MyRateCard ratecard = new Gson()。fromJson(response.toString(),MyRateCard.class); – blb007