将 JSONObject 转换为 JSONArray 时出现问题

Problems converting JSONObject to JSONArray

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

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

这是我的 JSON解析器:

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
    }
}

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

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

然后你可以很容易地得到如下值...

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

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

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

但在你的字符串中是一个 JSON 对象 "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
}

所以使用下面的代码:

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

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

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

或获取字符串类型:

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

试试这个。

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; 
}

首先,在尝试解析 Json 格式的数据时,您应该能够区分 JsonObjectsJson数组。有很多工具可以做到这一点,但我推荐 :

http://jsonviewer.stack.hu/

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

http://pojo.sodhanalibrary.com/

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

JSONObject ratesObject = jObj.getJSONIObject("rates");
String aud = ratesObject.getString("AUD");