将JSONObject转换为JSONArray时出现问题

时间:2016-03-01 04:12:53

标签: java android android-studio android-json gson

所以我的应用程序有一个小错误阻止我进行货币转换。

03-01 03:56:05.810 18853-18870/com.example.justin.currencyconverter20 D/JSON Parser: result: {"base":"SGD","date":"2016-02-29","rates":{"AUD":0.99576,"BGN":1.2762,"BRL":2.8316,"CAD":0.96359,"CHF":0.71217,"CNY":4.6559,"CZK":17.655,"DKK":4.868,"GBP":0.51276,"HKD":5.5237,"HRK":4.9764,"HUF":203.11,"IDR":9500.4,"ILS":2.7769,"INR":48.537,"JPY":80.352,"KRW":879.31,"MXN":12.92,"MYR":2.9931,"NOK":6.2018,"NZD":1.0804,"PHP":33.68,"PLN":2.8413,"RON":2.9205,"RUB":53.927,"SEK":6.0828,"THB":25.336,"TRY":2.1059,"USD":0.71047,"ZAR":11.391,"EUR":0.65253}}
03-01 03:56:05.811 18853-18870/com.example.justin.currencyconverter20 E/JSON Parser: Error parsing data org.json.JSONException: Value {"AUD":0.99576,"BGN":1.2762,"BRL":2.8316,"CAD":0.96359,"CHF":0.71217,"CNY":4.6559,"CZK":17.655,"DKK":4.868,"GBP":0.51276,"HKD":5.5237,"HRK":4.9764,"HUF":203.11,"IDR":9500.4,"ILS":2.7769,"INR":48.537,"JPY":80.352,"KRW":879.31,"MXN":12.92,"MYR":2.9931,"NOK":6.2018,"NZD":1.0804,"PHP":33.68,"PLN":2.8413,"RON":2.9205,"RUB":53.927,"SEK":6.0828,"THB":25.336,"TRY":2.1059,"USD":0.71047,"ZAR":11.391,"EUR":0.65253} at rates of type org.json.JSONObject cannot be converted to JSONArray

这是我的JSONParser:

package com.example.justin.currencyconverter20;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;

public class JSONParser {

    String charset = "UTF-8";
    HttpURLConnection conn;
    DataOutputStream wr;
    StringBuilder result;
    URL urlObj;
    JSONArray Obj = null;
    StringBuilder sbParams;
    String paramsString;


    public JSONArray makeHttpRequest(String url, String method) {

        int i = 0;

        if(method.equals("GET")){

            try {
                urlObj = new URL(url);

                conn = (HttpURLConnection) urlObj.openConnection();

                conn.setDoOutput(false);

                conn.setRequestMethod("GET");

                conn.setConnectTimeout(15000);

                conn.connect();

            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        try {
            //Receive the response from the server
            InputStream in = new BufferedInputStream(conn.getInputStream());
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            result = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                result.append(line);
            }

            Log.d("JSON Parser", "result: " + result.toString());

        } catch (IOException e) {
            e.printStackTrace();
        }

        conn.disconnect();

        // try parse the string to a JSON object
        try {
            JSONObject jObj = new JSONObject(result.toString());
            Obj = jObj.getJSONArray("rates");
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON Object
        return Obj;
    }
}

任何帮助将不胜感激。注意:我对此仍然相当新,所以请尽可能简化,非常感谢。

编辑: 好的,我正在使用的JSON api是http://api.fixer.io/latest?base=SGD,我使用www.jsonlint.com将其转换为JSON格式到以下JSON中:

{
    "base": "SGD",
    "date": "2016-02-29",
    "rates": {
        "AUD": 0.99576,
        "BGN": 1.2762,
        "BRL": 2.8316,
        "CAD": 0.96359,
        "CHF": 0.71217,
        "CNY": 4.6559,
        "CZK": 17.655,
        "DKK": 4.868,
        "GBP": 0.51276,
        "HKD": 5.5237,
        "HRK": 4.9764,
        "HUF": 203.11,
        "IDR": 9500.4,
        "ILS": 2.7769,
        "INR": 48.537,
        "JPY": 80.352,
        "KRW": 879.31,
        "MXN": 12.92,
        "MYR": 2.9931,
        "NOK": 6.2018,
        "NZD": 1.0804,
        "PHP": 33.68,
        "PLN": 2.8413,
        "RON": 2.9205,
        "RUB": 53.927,
        "SEK": 6.0828,
        "THB": 25.336,
        "TRY": 2.1059,
        "USD": 0.71047,
        "ZAR": 11.391,
        "EUR": 0.65253
    }
}

4 个答案:

答案 0 :(得分:1)

如果rate是一个数组,那么代码如下:

 JSONArray Objs = jObj.getJSONArray("rates");

但是在你的字符串中是一个JSON对象"率":

{
  "AUD": 0.99576,
  "BGN": 1.2762,
  "BRL": 2.8316,
  "CAD": 0.96359,
  "CHF": 0.71217,
  "CNY": 4.6559,
  "CZK": 17.655,
  "DKK": 4.868,
  "GBP": 0.51276,
  "HKD": 5.5237,
  "HRK": 4.9764,
  "HUF": 203.11,
  "IDR": 9500.4,
  "ILS": 2.7769,
  "INR": 48.537,
  "JPY": 80.352,
  "KRW": 879.31,
  "MXN": 12.92,
  "MYR": 2.9931,
  "NOK": 6.2018,
  "NZD": 1.0804,
  "PHP": 33.68,
  "PLN": 2.8413,
  "RON": 2.9205,
  "RUB": 53.927,
  "SEK": 6.0828,
  "THB": 25.336,
  "TRY": 2.1059,
  "USD": 0.71047,
  "ZAR": 11.391,
  "EUR": 0.65253
}

所以请使用以下代码:

JSONObe Obj = jObj.getJSONObject("rates");

您可以通过以下方式访问Obj的任何字段:

Double php = Obj.getDouble("PHP");

或获取字符串类型:

String php = Obj.getString("PHP");

答案 1 :(得分:0)

json字符串中没有JSON数组。所以你必须得到另一个JSON对象。

JSONObject ratesObject = jObj.getJSONIObject("rates");

然后您可以轻松获得以下值......

Double aud = ratesObject.getDouble("AUD");
Double bgn = ratesObject.getDouble("BGN");

答案 2 :(得分:0)

试试这个。

public JSONObject makeHttpRequest(String url, String method) {
 try {
            JSONObject jObj = new JSONObject(result.toString());
             JSONObject jObj2 = jObj.getJSONObject("rates");
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }   
return jObj2; 
}

答案 3 :(得分:0)

首先,在尝试解析Json格式的数据时,您应该能够区分 JsonObjects JsonArrays 。有很多工具,但我建议:

http://jsonviewer.stack.hu/

其次,您可以使用 Gson 等库将json格式的数据转换为java POJO类和对象。为此,您可以使用以下工具,它将生成您需要为使用Gson创建的等效POJO类:

http://pojo.sodhanalibrary.com/

最后,您应该在代码中进行以下更改以删除错误:

if (!String.prototype.trim)
{
    String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };
}