无法解析JsonObject - 错误' String无法转换为JSONObject'

时间:2017-08-28 06:24:16

标签: java android json parsing

这是我再次尝试但无济于事......

我将String切换为double并更改hashmap中的值以适应双精度。仍然得到相同的错误.....很难找到只是拉一个没有数组

的jsonObject的例子

这里有什么问题???

{         " base":" EUR",         " date":" 2017-08-25",         "率":{             "澳元":1.4919,             " BGN":1.9558,             " BRL":3.7045,             " CAD":1.4769,             "瑞士法郎":1.139,             " CNY":7.8596,             " CZK":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,             " NOK":9.2278,             " NZD":1.6363,             " PHP":60.327,             " PLN":4.2598,             " RON":4.5983,             " RUB":69.831,             " SEK":9.5053,             " SGD":1.6055,             " THB":39.332,             "尝试":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);

5 个答案:

答案 0 :(得分:4)

  

String无法转换为JSONObject

问题

JSONArray array = new JSONArray();

为什么 JSONArray

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

Sol - 将 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”);

答案 1 :(得分:1)

您没有方括号,因此您根本不需要JSONArray相关的类或方法。

使用jsonObj2.getString("AUD")

如果您确实需要数字,请使用getDouble("AUD")

答案 2 :(得分:0)

将字符串转换为Json

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

从JSONObject获取String

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

答案 3 :(得分:0)

您的费率为double,但您致电getString()并将其存储在int

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

rates是一个jsonObject。无需循环,只需调用它并通过提供密钥来访问每个速率。

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

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

答案 4 :(得分:0)

您只需使用简单的步骤即可使用Google的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());