解析json抛出null对象

时间:2012-03-30 06:17:18

标签: java json

JSON INPUT(我已剪切并粘贴它,因此括号中可能会出现一些错误。)

 {"results":{"quote":[{"date":"2010-03-10","Date":"2010-03-10","Open":"16.51","High":"16.94","Low":"16.51","Close":"16.79","Volume":"33088600","Adj_Close":"16.79"},
{"date":"2010-03-09","Date":"2010-03-09","Open":"16.41","High":"16.72","Low":"16.40","Close":"16.53","Volume":"20755200","Adj_Close":"16.53"},
{"date":"2010-03-08","Date":"2010-03-08","Open":"16.32","High":"16.61","Low":"16.30","Close":"16.52","Volume":"30554000","Adj_Close":"16.52"},
{"date":"2010-03-05","Date":"2010-03-05","Open":"15.89","High":"16.38","Low":"15.89","Close":"16.06","Volume":"21415000","Adj_Close":"16.06"},
{"date":"2010-03-04","Date":"2010-03-04","Open":"15.55","High":"15.85","Low":"15.52","Close":"15.81","Volume":"22906000","Adj_Close":"}]}

JAVA CODE

System.out.println(inputLine1);
            JSONObject json = (JSONObject) JSONSerializer.toJSON( inputLine1 ); 
            JSONObject pilot = json.getJSONObject("results");        

            JSONArray jsonMainArr = pilot.getJSONArray("quote");  

            for (int i = 0; i < jsonMainArr.size(); i++) 
            {  // **line 2**  
                JSONObject childJSONObject = jsonMainArr.getJSONObject(i);   
                String date = childJSONObject.getString("date");  
                String Date=childJSONObject.getString("Date");
                String Open=childJSONObject.getString("Open");
                String Close=childJSONObject.getString("Close");
                String High=childJSONObject.getString("High");
                String low=childJSONObject.getString("Low");
                String Volume=childJSONObject.getString("Volume");
                String Adj_close=childJSONObject.getString("Adj_Close");

                System.out.println(date);
                System.out.println(Open);


            }  

它表示一个空对象。我做错了什么。

1 个答案:

答案 0 :(得分:0)

JSON不对。最后"未关闭:

{"results":{"quote":[..., {..., "Adj_Close":"}]}

这会导致无法解析quote的值,因此当您需要数组时,库会返回null