2017-08-28 88 views
0

這裏是我又試了一次,但無濟於事....故障解析的JSONObject - 錯誤「字符串不能轉換爲JSONObject的」

我切換字符串將翻一番,改變以適應雙打作爲HashMap中的值好。仍然得到同樣的錯誤.....它很難找到只是拉一個沒有數組的jsonObject的示例

這裏有什麼問題?

{ 「基地」: 「EUR」, 「日期」: 「2017年8月25日」, 「速度」:{ 「AUD」:1.4919, 「BGN」:1.9558, 「BRL 「:3.7045, 「CAD」:1.4769, 「CHF」:1.139, 「萬元」:7.8596, 「捷克克朗」:26.084, 「DKK」:7.4391, 「GBP」:0.92083, 「HKD」 :9.2372, 「HRK」:7.414, 「HUF」:304.68, 「IDR」:15758.0, 「ILS」:4.2453, 「INR」:75.598, 「JPY」:129.59, 「KRW」:1327.0, 「MXN」:20.844, 「MYR」:5.0456, 「諾」:9.2278, 「NZD」:1.6363, 「PHP」:60.327, 「PLN」:4.2598, 「RON」:4.5983, 「蹭」:69.831, 「SEK」:9.5053, 「SGD」:1.6055, 「THB」:39.332, 「 TRY「:4.108, 」USD「:1.1808, 」ZAR「:15.549 }}


if (jsonStr != null) { 
      try { 
       JSONObject jsonObj = new JSONObject(jsonStr); 


       JSONObject jsonObj2 = jsonObj.getJSONObject("rates"); 


       double australia = jsonObj2.getDouble("AUD"); 
       double bulgarian = jsonObj2.getDouble("BDN"); 
       double brazil = jsonObj2.getDouble("BRL"); 
       double canadian = jsonObj2.getDouble("CAD"); 
       double swissfrank = jsonObj2.getDouble("CHF"); 
       double chinnese = jsonObj2.getDouble("CNY"); 
       double Czech = jsonObj2.getDouble("CZK"); 
       double Danish = jsonObj2.getDouble("DKK"); 
       double british = jsonObj2.getDouble("GBP"); 
       double hongkong = jsonObj2.getDouble("HKD"); 
       double croatian = jsonObj2.getDouble("HRK"); 
       double hungarian = jsonObj2.getDouble("HUF"); 
       double indonesian = jsonObj2.getDouble("IDR"); 
       double israeli = jsonObj2.getDouble("ILS"); 
       double indian = jsonObj2.getDouble("INR"); 
       double japan = jsonObj2.getDouble("JPY"); 
       double korean = jsonObj2.getDouble("KRW"); 
       double mexican = jsonObj2.getDouble("MXN"); 
       double malaysian = jsonObj2.getDouble("MYR"); 
       double norwegian = jsonObj2.getDouble("NOK"); 
       double newzealand = jsonObj2.getDouble("NZD"); 
       double philippino = jsonObj2.getDouble("PHP"); 
       double polish = jsonObj2.getDouble("PLN"); 
       double romanian = jsonObj2.getDouble("RON"); 
       double russian = jsonObj2.getDouble("RUB"); 
       double swedish = jsonObj2.getDouble("SEK"); 
       double singapore = jsonObj2.getDouble("SGD"); 
       double thai = jsonObj2.getDouble("THB"); 
       double turkish = jsonObj2.getDouble("TRY"); 
       double usa = jsonObj2.getDouble("USD"); 
       double southafrican = jsonObj2.getDouble("ZAR"); 

       // tmp hash map for single contact 
       HashMap<String, Double> contact = new HashMap<>(); 

       // adding each child node to HashMap key => value 
       contact.put("AUD", australia); 
       contact.put("BDN", bulgarian); 
       contact.put("BRL", brazil); 
       contact.put("CAD", canadian); 
       contact.put("CHF", swissfrank); 
       contact.put("CNY", chinnese); 
       contact.put("CZK", Czech); 
       contact.put("DKK", Danish); 
       contact.put("GBP", british); 
       contact.put("HKD", hongkong); 
       contact.put("HRK", croatian); 
       contact.put("HUF", hungarian); 
       contact.put("IDR", indonesian); 
       contact.put("ILS", israeli); 
       contact.put("INR", indian); 
       contact.put("JPY", japan); 
       contact.put("KRW", korean); 
       contact.put("MXN", mexican); 
       contact.put("MYR", malaysian); 
       contact.put("NOK", norwegian); 
       contact.put("NZD", newzealand); 
       contact.put("PHP", philippino); 
       contact.put("PLN", polish); 
       contact.put("RON", romanian); 
       contact.put("RUB", russian); 
       contact.put("SEK", swedish); 
       contact.put("SGD", singapore); 
       contact.put("THB", thai); 
       contact.put("TRY", turkish); 
       contact.put("USA", usa); 
       contact.put("ZAR", southafrican); 


       // adding contact to contact list 

       contactList.add(contact); 
+1

你的利率不是一個數組類型的對象類型。 –

+0

'getString(「AUD」)。toString()'是多餘的。你已經得到一個字符串 –

+0

嘗試Double australia = ratesObject.getDouble(「AUD」); –

回答

4

字符串不能轉換到的JSONObject

問題

JSONArray array = new JSONArray(); 

爲什麼JSONArray

"rates": { 
     "AUD": 1.4919, 
     "BGN": 1.9558, 

溶膠 - 使用IteratorJSONObject#keys

  Iterator<String> iteratorObj = jsonObj2.keys(); 
      ArrayList<String> al_getAllKeys=new ArrayList<String>(); 
      while (iteratorObj.hasNext()) 
      { 
       String key = iteratorObj.next(); 
       .......... 
       System.out.println("Key_VALUE: " + key + "------>" + jsonObj2.getString(key)); 

      } 

FYI

爲什麼int australia? 。它應該是double australia

double australia = ratesObject。getDouble( 「AUD」);

+0

@MarAvFe同意。在這種情況下,我使用'Iterator' –

+0

JSONObject jsonObj = new JSONObject(jsonStr); JSONObject jsonObj2 = jsonObj.getJSONObject(「rates」); double australia = jsonObj2.getDouble(「AUD」); double bulgarian = jsonObj2.getDouble(「BDN」);仍然給我同樣的錯誤.... – Davidito

1

你沒有方括號,所以你根本不需要JSONArray相關的類或方法。

使用jsonObj2.getString("AUD")

如果你真的想號碼,使用getDouble("AUD")

+0

我試過沒有它也JSONObject jsonObj = new JSONObject(jsonStr); JSONObject jsonObj2 = jsonObj.getJSONObject(「rates」); //通過所有聯繫人 爲循環(INT I = 0; I Davidito

+0

你並不需要一個循環可言,要麼 –

+1

一個'JSONObject'是基本相同的一個HashMap –

0

使用GSON庫,讓您的費率。

編譯 'org.immutables:GSON:2.5.6'

public class Main { 

@SerializedName("base") 
@Expose 
private String base; 
@SerializedName("date") 
@Expose 
private String date; 
@SerializedName("rates") 
@Expose 
private Rates rates; 

public String getBase() { 
return base; 
} 

public void setBase(String base) { 
this.base = base; 
} 

public String getDate() { 
return date; 
} 

public void setDate(String date) { 
this.date = date; 
} 

public Rates getRates() { 
return rates; 
} 

public void setRates(Rates rates) { 
this.rates = rates; 
} 

} 


public class Rates { 

@SerializedName("AUD") 
@Expose 
private Double aUD; 
@SerializedName("BGN") 
@Expose 
private Double bGN; 
@SerializedName("BRL") 
@Expose 
private Double bRL; 
@SerializedName("CAD") 
@Expose 
private Double cAD; 
@SerializedName("CHF") 
@Expose 
private Double cHF; 
@SerializedName("CNY") 
@Expose 
private Double cNY; 
@SerializedName("CZK") 
@Expose 
private Double cZK; 
@SerializedName("DKK") 
@Expose 
private Double dKK; 
@SerializedName("GBP") 
@Expose 
private Double gBP; 
@SerializedName("HKD") 
@Expose 
private Double hKD; 
@SerializedName("HRK") 
@Expose 
private Double hRK; 
@SerializedName("HUF") 
@Expose 
private Double hUF; 
@SerializedName("IDR") 
@Expose 
private Double iDR; 
@SerializedName("ILS") 
@Expose 
private Double iLS; 
@SerializedName("INR") 
@Expose 
private Double iNR; 
@SerializedName("JPY") 
@Expose 
private Double jPY; 
@SerializedName("KRW") 
@Expose 
private Double kRW; 
@SerializedName("MXN") 
@Expose 
private Double mXN; 
@SerializedName("MYR") 
@Expose 
private Double mYR; 
@SerializedName("NOK") 
@Expose 
private Double nOK; 
@SerializedName("NZD") 
@Expose 
private Double nZD; 
@SerializedName("PHP") 
@Expose 
private Double pHP; 
@SerializedName("PLN") 
@Expose 
private Double pLN; 
@SerializedName("RON") 
@Expose 
private Double rON; 
@SerializedName("RUB") 
@Expose 
private Double rUB; 
@SerializedName("SEK") 
@Expose 
private Double sEK; 
@SerializedName("SGD") 
@Expose 
private Double sGD; 
@SerializedName("THB") 
@Expose 
private Double tHB; 
@SerializedName("TRY") 
@Expose 
private Double tRY; 
@SerializedName("USD") 
@Expose 
private Double uSD; 
@SerializedName("ZAR") 
@Expose 
private Double zAR; 

public Double getAUD() { 
return aUD; 
} 

public void setAUD(Double aUD) { 
this.aUD = aUD; 
} 

public Double getBGN() { 
return bGN; 
} 

public void setBGN(Double bGN) { 
this.bGN = bGN; 
} 

public Double getBRL() { 
return bRL; 
} 

public void setBRL(Double bRL) { 
this.bRL = bRL; 
} 

public Double getCAD() { 
return cAD; 
} 

public void setCAD(Double cAD) { 
this.cAD = cAD; 
} 

public Double getCHF() { 
return cHF; 
} 

public void setCHF(Double cHF) { 
this.cHF = cHF; 
} 

public Double getCNY() { 
return cNY; 
} 

public void setCNY(Double cNY) { 
this.cNY = cNY; 
} 

public Double getCZK() { 
return cZK; 
} 

public void setCZK(Double cZK) { 
this.cZK = cZK; 
} 

public Double getDKK() { 
return dKK; 
} 

public void setDKK(Double dKK) { 
this.dKK = dKK; 
} 

public Double getGBP() { 
return gBP; 
} 

public void setGBP(Double gBP) { 
this.gBP = gBP; 
} 

public Double getHKD() { 
return hKD; 
} 

public void setHKD(Double hKD) { 
this.hKD = hKD; 
} 

public Double getHRK() { 
return hRK; 
} 

public void setHRK(Double hRK) { 
this.hRK = hRK; 
} 

public Double getHUF() { 
return hUF; 
} 

public void setHUF(Double hUF) { 
this.hUF = hUF; 
} 

public Double getIDR() { 
return iDR; 
} 

public void setIDR(Double iDR) { 
this.iDR = iDR; 
} 

public Double getILS() { 
return iLS; 
} 

public void setILS(Double iLS) { 
this.iLS = iLS; 
} 

public Double getINR() { 
return iNR; 
} 

public void setINR(Double iNR) { 
this.iNR = iNR; 
} 

public Double getJPY() { 
return jPY; 
} 

public void setJPY(Double jPY) { 
this.jPY = jPY; 
} 

public Double getKRW() { 
return kRW; 
} 

public void setKRW(Double kRW) { 
this.kRW = kRW; 
} 

public Double getMXN() { 
return mXN; 
} 

public void setMXN(Double mXN) { 
this.mXN = mXN; 
} 

public Double getMYR() { 
return mYR; 
} 

public void setMYR(Double mYR) { 
this.mYR = mYR; 
} 

public Double getNOK() { 
return nOK; 
} 

public void setNOK(Double nOK) { 
this.nOK = nOK; 
} 

public Double getNZD() { 
return nZD; 
} 

public void setNZD(Double nZD) { 
this.nZD = nZD; 
} 

public Double getPHP() { 
return pHP; 
} 

public void setPHP(Double pHP) { 
this.pHP = pHP; 
} 

public Double getPLN() { 
return pLN; 
} 

public void setPLN(Double pLN) { 
this.pLN = pLN; 
} 

public Double getRON() { 
return rON; 
} 

public void setRON(Double rON) { 
this.rON = rON; 
} 

public Double getRUB() { 
return rUB; 
} 

public void setRUB(Double rUB) { 
this.rUB = rUB; 
} 

public Double getSEK() { 
return sEK; 
} 

public void setSEK(Double sEK) { 
this.sEK = sEK; 
} 

public Double getSGD() { 
return sGD; 
} 

public void setSGD(Double sGD) { 
this.sGD = sGD; 
} 

public Double getTHB() { 
return tHB; 
} 

public void setTHB(Double tHB) { 
this.tHB = tHB; 
} 

public Double getTRY() { 
return tRY; 
} 

public void setTRY(Double tRY) { 
this.tRY = tRY; 
} 

public Double getUSD() { 
return uSD; 
} 

public void setUSD(Double uSD) { 
this.uSD = uSD; 
} 

public Double getZAR() { 
return zAR; 
} 

public void setZAR(Double zAR) { 
this.zAR = zAR; 
} 

} 

這裏是你的POJO類。

+0

這是不必要的冗長,並且不能爲其他貨幣進行縮放 –

+1

另外,這不是官方的Gson庫。 http://search.maven.org/#artifactdetails%7Ccom.google.code.gson%7Cgson%7C2.8.1%7C –

0

將字符串轉換成JSON

JSONObject jsonObj = new JSONObject(cursorUsers.getString("String"); 

當從JSONObject的

String str=jsonObj.getString("parseObjectName")); 
0

獲取字符串你率在double,但你打電話getString()並將它們存儲在int

你的JsonArray array是空的,你正在循環着它!

rates是一個jsonObject。無需循環,只需調用它並通過提供密鑰來訪問每個速率。

double aud = jsonObject2.getDouble("AUD"); 

double bnd = jsonObject2.getDouble("BND"); 
+0

你無法循環瀏覽jsonObject,從來沒有。您需要保留一個單獨的密鑰列表,以便爲jsonObject2.getDouble(key)提供密鑰。 –

0

您可以使用谷歌的GSON庫,簡單的步驟去做,試試下面的代碼

String jsonString = "Your JSON string"; 

Gson gson = new Gson(); 

//Convert Json String to Json Object without a POJO class 
JsonObject jsonObj = gson.fromJson (jsonString, JsonElement.class).getAsJsonObject(); 

//Get rates object and convert it to hashmap (For easy access) 
HashMap<String, String> ratesMap = gson.fromJson(jsonObj.get("rates").getAsString(), new TypeToken<HashMap<String, String>>(){}.getType()); 
相關問題