2011-06-09 73 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