2013-03-19 63 views
0

當我將json數據從Android傳遞到服務器時,它在解析時在服務器上發生錯誤。 org.json.JSONException:一個JSONObject文本必須以 '{' 以1字符2線1]JSON:解析服務器上的json數據時出現問題

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 DAT一個服務器的樣子

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 
+0

發表您的JSON。 – rajeshwaran 2013-03-19 05:46:16

+0

發佈完整的logcat消息或你在'System.out.println(sb.toString())'行中得到什麼日誌 – 2013-03-19 05:47:37

+0

http://stackoverflow.com/questions/4773663/jsonobject-text-must-begin-with – rajeshwaran 2013-03-19 05:48:22

回答

1

你可以試試下面的代碼解析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; 
} 
+0

我在JSONObject的問題json = new JSONObject(jsonstring);我無法將我的字符串解析爲JSONObject,請參閱代碼 – 2013-03-19 06:04:44