我如何解析这个JSON而不是异常?

时间:2011-06-09 01:23:35

标签: java android json arrays

我有以下JSON:

    [
  {
    "outcome": "Success",
    "message": "",
    "identity": "",
    "delay": "0",
    "symbol": "AAPL",
    "companyname": "Apple Inc.",
    "date": "Jun 08",
    "time": " 4:52 PM EDT",
    "open": "330.88",
    "close": "332",
    "previousclose": "332.04",
    "high": "334.8",
    "low": "330.51",
    "last": "332",
    "change": "-0.04",
    "percentchange": "-0.012",
    "volume": "1239421",
    "created_at": "1307581193"
  },
  {
    "outcome": "Success",
    "message": "",
    "identity": "",
    "delay": "0",
    "symbol": "GOOG",
    "companyname": "Google Inc.",
    "date": "Jun 08",
    "time": " 3:59 PM EDT",
    "open": "516.76",
    "close": "519.28",
    "previousclose": "519.03",
    "high": "521.14",
    "low": "515.79",
    "last": "519.28",
    "change": "0.25",
    "percentchange": "0.048",
    "volume": "229886",
    "created_at": "1307576900"
  }
]

这是我的Java:

JSONArray jquotes = new JSONArray(json.toString());
                    if (jquotes.length() > 0) {
                        for (int i = 0; i < jquotes.length(); i++) {

                            JSONObject quote = jquotes.getJSONObject(i);

                            Quote myQuote = new Quote();
                            myQuote.setName(quote.getString("companyname"));
                            myQuote.setSymbol(quote.getString("symbol"));
                            myQuote.setLastTradePriceOnly(quote.getString("last"));
                            myQuote.setChange(quote.getString("change"));
                            myQuote.setPercentChange(quote.getString("percentchange"));

                            quotesAdapter.add(myQuote);
                        }
                    }

我得到了例外:

value of type org.json.JSONArray cannot be converted to JSONObject
at org.json.JSON.typeMismatch(JSON.java:107)

2 个答案:

答案 0 :(得分:3)

你拥有的实际上是一个JSONArray(注意开头的括号),所以:

JSONArray array = new JSONArray (jsonString);
JSONObject obj = array.getJSONObject(0);
String outcome = obj.getString("outcome");
int delay = getInt("delay");
// etc.

API非常简单。您可以阅读详细信息here

答案 1 :(得分:0)

正如当前问题的内容所示,所提供的JSON反序列化而没有提供代码的错误。

对于下面的工作代码,我所做的只是复制并粘贴问题中的内容,并按预期运行。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

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

public class Foo
{
  public static void main(String[] args) throws Exception
  {
    String jsonInput = readJsonInput(new File("input.json"));
    JSONArray jquotes = new JSONArray(jsonInput);
    int length = jquotes.length();
    List<Quote> quotes = new ArrayList<Quote>(length);
    if (length > 0)
    {
      for (int i = 0; i < length; i++)
      {
        JSONObject quote = jquotes.getJSONObject(i);

        Quote myQuote = new Quote();
        myQuote.setName(quote.getString("companyname"));
        myQuote.setSymbol(quote.getString("symbol"));
        myQuote.setLastTradePriceOnly(quote.getString("last"));
        myQuote.setChange(quote.getString("change"));
        myQuote.setPercentChange(quote.getString("percentchange"));

        quotes.add(myQuote);
      }
    }
    System.out.println(quotes);
  }

  private static String readJsonInput(File inputFile) throws Exception
  {
    StringBuilder result = new StringBuilder();
    BufferedReader reader = new BufferedReader(new FileReader(inputFile));
    for (String line = null; (line = reader.readLine()) != null;)
    {
      result.append(line);
    }
    return result.toString();
  }
}

class Quote
{
  private String name;
  private String percentChange;
  private String change;
  private String lastTradePriceOnly;
  private String symbol;

  void setName(String name)
  {
    this.name = name;
  }

  void setPercentChange(String percentChange)
  {
    this.percentChange = percentChange;
  }

  void setChange(String change)
  {
    this.change = change;
  }

  void setLastTradePriceOnly(String lastTradePriceOnly)
  {
    this.lastTradePriceOnly = lastTradePriceOnly;
  }

  void setSymbol(String symbol)
  {
    this.symbol = symbol;
  }

  @Override
  public String toString()
  {
    return String.format(
        "{Quote: name=%s, percentChange=%s, change=%s, lastTradePriceOnly=%s, symbol=%s}",
        name, percentChange, change, lastTradePriceOnly, symbol);
  }
}