簡體   English   中英

JSON:在服務器上解析JSON數據時出現問題

[英]JSON: issue in parsing json data at server

當我將json數據從Android傳遞到服務器時,其解析時在服務器上出現以下錯誤。 org.json.JSONException:JSONObject文本必須在1 [字符2第1行]處以“ {”開頭

Servlet代碼為:

protected void doProcess(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        BufferedReader br = request.getReader();

        String sCurrentLine;
        StringBuilder sb = new StringBuilder();

        while ((sCurrentLine = br.readLine()) != null) {
            sb.append(sCurrentLine);
        }

        System.out.println(sb.toString().substring(4));

        try {
            JSONArray jArray = new JSONArray(sb.toString().substring(4));
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject json_data = jArray.getJSONObject(i);
                json_data.getString("dwsList");
            }

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

    }

Android部分是:

ArrayList<STRModel> strlist = StrDbHelper
                    .getPendingStrlist(Splash.this);
            System.out.println("--------Inside------" + strlist.size());

            if (strlist != null) {
                String URL = "http://ip:8080/Admin/upload";
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(URL);

                try {
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                            2);
                    nameValuePairs.add(new BasicNameValuePair("str", new Gson()
                            .toJson(strlist)));


                    System.out.println(new Gson().toJson(strlist));



                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    HttpResponse response = httpclient.execute(httppost);

                    Log.i("Response", response.getEntity().getContent()
                            .toString());

                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                    return false;
                } catch (IOException e) {
                    e.printStackTrace();
                    return false;
                }

            }

我從客戶處傳來:

[
{"dwsList":
[
{"farmer_name":"JINJU","dws_date":"2013-03-08","farmer_code":"10004","dws_num":"53","dws_id":4,"bag_weight":3000.0,"net_weight":2000.0,"selected":false,"str_id":0,"totalBags":20},
{"farmer_name":"KANNAN","dws_date":"2013-03-08","farmer_code":"10005","dws_num":"54","dws_id":5,"bag_weight":1500.0,"net_weight":1000.0,"selected":false,"str_id":0,"totalBags":10}
],
"str_total_weight":4000.0,"str_date":"2013-03-08","str_number":"1003","str_id":4,"str_total_bag":0,"status":0,"selected":false}
]

當我在服務器中打印Json數據時,它看起來像

 str=%5B%7B%22dwsList%22%3A%5B%7B%22farmer_name%22%3A%22JINJU%22%2C%22dws_date%22%3A%222013-03-08%22%2C%22farmer_code%22%3A%2210004%22%2C%22dws_num%22%3A%2253%22%2C%22dws_id%22%3A4%2C%22bag_weight%22%3A3000.0%2C%22net_weight%22%3A2000.0%2C%22selected%22%3Afalse%2C%22str_id%22%3A0%2C%22totalBags%22%3A20%7D%2C%7B%22farmer_name%22%3A%22KANNAN%22%2C%22dws_date%22%3A%222013-03-08%22%2C%22farmer_code%22%3A%2210005%22%2C%22dws_num%22%3A%2254%22%2C%22dws_id%22%3A5%2C%22bag_weight%22%3A1500.0%2C%22net_weight%22%3A1000.0%2C%22selected%22%3Afalse%2C%22str_id%22%3A0%2C%22totalBags%22%3A10%7D%5D%2C%22str_total_weight%22%3A4000.0%2C%22str_date%22%3A%222013-03-08%22%2C%22str_number%22%3A%221003%22%2C%22str_id%22%3A4%2C%22str_total_bag%22%3A0%2C%22status%22%3A0%2C%22selected%22%3Afalse%7D%5D

您可以嘗試以下代碼來解析JSON

{
"result": "success",
"countryCodeList":
[
{"countryCode":"00","countryName":"World Wide"},
{"countryCode":"kr","countryName":"Korea, Republic of"},
{"countryCode":"us","countryName":"United States"},
{"countryCode":"jp","countryName":"Japan"},
{"countryCode":"cn","countryName":"China"},
{"countryCode":"in","countryName":"India"}
]
}

public static ArrayList<Country> ParseJson(String jsonstring) {

    ArrayList<Country> arrCountries = new ArrayList<Country>();

    String status;
    String message = "";
    try {


        JSONObject json = new JSONObject(jsonstring);

        status = json.getString("result");

        if (status.equalsIgnoreCase("success")) {


            JSONArray nameArray = json.names();
            JSONArray valArray = json.toJSONArray(nameArray);

            JSONArray valArray1 = valArray.getJSONArray(1);

            valArray1.toString().replace("[", "");
            valArray1.toString().replace("]", "");

            int len = valArray1.length();

            for (int i = 0; i < valArray1.length(); i++) {

                Country country = new Country();
                JSONObject arr = valArray1.getJSONObject(i);

                country.setCountryCode(arr.getString("countryCode"));
                country.setCountryName(arr.getString("countryName"));
                arrCountries.add(country);
            }
        }

    } catch (JSONException e) {
        Log.e("JSON", "There was an error parsing the JSON", e);
    }
    return arrCountries;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM