有人可以帮我解决这个JSON解析吗?

时间:2016-07-04 13:07:09

标签: android json android-asynctask httpurlconnection

从JSON中检索单个元素时出现问题。喜欢:" t":" SBIN"

JSON-URL如下:http://finance.google.com/finance/info?q=BSE:SBIN

其他资源如下:

public class RefreshStock extends AsyncTask<String, String, List<StockModel>> {

    @Override
    protected List<StockModel> doInBackground(String... params) {

        try {
            url = new URL(params[0]);

            connection = (HttpURLConnection) url.openConnection();
            connection.connect();

            inputStream = connection.getInputStream();

            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

            stringBuffer = new StringBuffer();

            String line = "";

            while ((line = bufferedReader.readLine()) != null)
                stringBuffer.append(line + "\n");

            String data = stringBuffer.toString();

            return parseJSON(data);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (connection != null)
                connection.disconnect();
            try {
                if (bufferedReader != null)
                    bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    private List<StockModel> parseJSON(String json) {

        List<StockModel> stockModelList = new ArrayList<>();

        json = json.substring(3);
        //textView.setText(""+json);

        try {

            StockModel stockModel = new StockModel();
            JSONObject parentObject = new JSONObject();
            JSONArray jsonArray = parentObject.getJSONArray(json);

                JSONObject childObject = jsonArray.getJSONObject(0);
                String stockId = childObject.getString("id");
                String stockName = childObject.getString("t");
                String stockRate = childObject.getString("l");
                String stockChange = childObject.getString("c");
                String stockChangePercent = childObject.getString("cp");

            Toast.makeText( getApplicationContext(),""+stockRate, Toast.LENGTH_LONG).show();

            stockModel.setId(stockId);
            stockModel.setT(stockName);
            stockModel.setL(stockRate);
            stockModel.setC(stockChange);
            stockModel.setCp(stockChangePercent);
            stockModelList.add(stockModel);

            return stockModelList;
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return stockModelList;
    }

    protected void onPostExecute(List<StockModel> result) {
        super.onPostExecute(result);

        StockAdapter adapter = new StockAdapter(getApplicationContext(), R.layout.listrow, result);
        lvStock.setAdapter(adapter);
    }
}

logcat消息如下:

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ org.json.JSONException: No value for  [

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ {

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ "id": "13564339"

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ ,"t" : "SBIN"

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ ,"e" : "NSE"

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ ,"l" : "222.90"

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ ,"l_fix" : "222.90"

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ ,"l_cur" : "Rs.222.90"

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ ,"s": "0"

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ ,"ltt":"3:55PM GMT+5:30"

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ ,"lt" : "Jul 4, 3:55PM GMT+5:30"

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ ,"lt_dts" : "2016-07-04T15:55:45Z"

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ ,"c" : "+3.30"

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ ,"c_fix" : "3.30"

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ ,"cp" : "1.50"

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ ,"cp_fix" : "1.50"

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ ,"ccol" : "chg"

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ ,"pcls_fix" : "219.6"

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ }

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ ]

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ [ 07-04 
10:49:42.667 11270:11321 W/System.err ]
            at org.json.JSONObject.get(JSONObject.java:389)

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ at org.json.JSONObject.getJSONArray(JSONObject.java:584)

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ at org.example.km.http2json.MainActivity$RefreshStock.parseJSON(MainActivity.java:117)

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ at org.example.km.http2json.MainActivity$RefreshStock.doInBackground(MainActivity.java:87)

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ at org.example.km.http2json.MainActivity$RefreshStock.doInBackground(MainActivity.java:63)

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)

07-04 10:49:42.667  11270-11321/org.example.km.http2json W/System.err﹕ at java.lang.Thread.run(Thread.java:818)

谢谢:P

2 个答案:

答案 0 :(得分:0)

您的Json响应无效

替换

// [
{
"id": "13564339"
,"t" : "SBIN"
,"e" : "NSE"
,"l" : "222.90"
,"l_fix" : "222.90"
,"l_cur" : "Rs.222.90"
,"s": "0"
,"ltt":"3:55PM GMT+5:30"
,"lt" : "Jul 4, 3:55PM GMT+5:30"
,"lt_dts" : "2016-07-04T15:55:45Z"
,"c" : "+3.30"
,"c_fix" : "3.30"
,"cp" : "1.50"
,"cp_fix" : "1.50"
,"ccol" : "chg"
,"pcls_fix" : "219.6"
}
]

to 

[
{
"id": "13564339"
,"t" : "SBIN"
,"e" : "NSE"
,"l" : "222.90"
,"l_fix" : "222.90"
,"l_cur" : "Rs.222.90"
,"s": "0"
,"ltt":"3:55PM GMT+5:30"
,"lt" : "Jul 4, 3:55PM GMT+5:30"
,"lt_dts" : "2016-07-04T15:55:45Z"
,"c" : "+3.30"
,"c_fix" : "3.30"
,"cp" : "1.50"
,"cp_fix" : "1.50"
,"ccol" : "chg"
,"pcls_fix" : "219.6"
}
]

答案 1 :(得分:0)

json中的

错误。

删除//斜杠登录JSON