JSON:解析服务器上的json数据时出现问题
问题描述:
当我将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
答
你可以试试下面的代码解析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
发表您的JSON。 – rajeshwaran 2013-03-19 05:46:16
发布完整的logcat消息或你在'System.out.println(sb.toString())'行中得到什么日志 – 2013-03-19 05:47:37
http://stackoverflow.com/questions/4773663/jsonobject-text-must-begin-with – rajeshwaran 2013-03-19 05:48:22