2011-06-09 56 views
0

我有以下JSON:我该如何解析这个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) 
+0

顺便说一句,你完全可能要考虑GSON:http://code.google.com/p/google-gson/ – 2011-06-09 02:55:12

回答

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

你有什么实际上是一个JSONArray(注意在开始的括号中),所以:

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

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

+0

BTW,我忘了提,它的大小为1的数组,所以你只需要从中检索一个JSONObject。 – Aleadam 2011-06-09 01:56:50

+0

谢谢,几乎在那里。查看更新的代码。我已经有了一个JSON对象(这是响应),并且由于它是一个数组,我将toString(),然后创建一个数组。不知道这是否是正确的方法。 – 2011-06-09 02:00:53

+0

不,你是如何创建JSON对象的?很可能例外是因为这一点。 – Aleadam 2011-06-09 02:09:48