故障解析的JSONObject - 错误“字符串不能转换为JSONObject的”
这里是我又试了一次,但无济于事....故障解析的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);
字符串不能转换到的JSONObject
问题
JSONArray array = new JSONArray();
为什么JSONArray
"rates": {
"AUD": 1.4919,
"BGN": 1.9558,
溶胶 - 使用Iterator与JSONObject#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”);
@MarAvFe同意。在这种情况下,我使用'Iterator' –
JSONObject jsonObj = new JSONObject(jsonStr); JSONObject jsonObj2 = jsonObj.getJSONObject(“rates”); double australia = jsonObj2.getDouble(“AUD”); double bulgarian = jsonObj2.getDouble(“BDN”);仍然给我同样的错误.... – Davidito
你没有方括号,所以你根本不需要JSONArray
相关的类或方法。
使用jsonObj2.getString("AUD")
如果你真的想号码,使用getDouble("AUD")
我试过没有它也JSONObject jsonObj = new JSONObject(jsonStr); JSONObject jsonObj2 = jsonObj.getJSONObject(“rates”); //通过所有联系人 为循环(INT I = 0; I
你并不需要一个循环可言,要么 –
一个'JSONObject'是基本相同的一个HashMap –
使用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类。
这是不必要的冗长,并且不能为其他货币进行缩放 –
另外,这不是官方的Gson库。 http://search.maven.org/#artifactdetails%7Ccom.google.code.gson%7Cgson%7C2.8.1%7C –
将字符串转换成JSON
JSONObject jsonObj = new JSONObject(cursorUsers.getString("String");
当从JSONObject的
String str=jsonObj.getString("parseObjectName"));
获取字符串你率在double
,但你打电话getString()
并将它们存储在int
!
你的JsonArray array
是空的,你正在循环着它!
rates
是一个jsonObject。无需循环,只需调用它并通过提供密钥来访问每个速率。
double aud = jsonObject2.getDouble("AUD");
double bnd = jsonObject2.getDouble("BND");
你无法循环浏览jsonObject,从来没有。您需要保留一个单独的密钥列表,以便为jsonObject2.getDouble(key)提供密钥。 –
您可以使用谷歌的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());
你的利率不是一个数组类型的对象类型。 –
'getString(“AUD”)。toString()'是多余的。你已经得到一个字符串 –
尝试Double australia = ratesObject.getDouble(“AUD”); –