我正在使用Android应用中的webservice。我无法在app中解析以下响应。
总是给出org.json.JSONException:Value [{ “METER_READING”: “15”, “UTILITY_PLAN”: “1”, “UNAME”: “vinayak@triffort.com”, “kwh_usage”: “3”, “meter_reading_date”: “2014年2月13日”, “ESID”: “ABC”, “METER_ID”: “abc100”}] at java.lang.String类型的数据无法转换为JSONArray。
以下是我的代码:
StringEntity entity = new StringEntity(jsonObject.toString(), HTTP.UTF_8);
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
BufferedReader reader =new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String jsonResultStr = reader.readLine();
JSONObject jObject = new JSONObject(jsonResultStr);
JSONArray jArray = jObject.optJSONArray("data");
我从webservice获得以下回复
{ “数据”: “[{\” METER_READING \ “:\” 25 \”,\ “UTILITY_PLAN \”:\ “1 \” \ “UNAME \”:\ “vinayak@triffort.com \” ,\ “kwh_usage \”:\ “9 \”,\ “meter_reading_date \”:\ “2014年2月13日\”,\ “ESID \”:\ “ABC \”,\ “METER_ID \”:\“abc100 \ “}]”}
答案 0 :(得分:0)
尝试这个简单的代码:
JSONObject o = new JSONObject(new JSONTokener(postResponse));
JSONArray ja = o.getJSONArray("data");
修改强>
感谢@McDowell进行观察
new JSONArray(new JSONTokener(jObject.optString("data")));
答案 1 :(得分:0)
我收到了以下回复
{ "data":"[{\"METER_READING\":\"25...}]" }
数据的值不是数组;这是一个字符串。该字符串是有效的JSON,你可以解析,但为什么服务会这样做是不清楚的。
所以这应该有效:
JSONObject jObject = new JSONObject(jsonResultStr);
String parseMeAgain = jObject.optString("data");
答案 2 :(得分:0)
尝试使用以下内容:
jsonResultStr = jsonResultStr.replace( "\\", "" ).replaceAll( "\"\\[", "[" ).replaceAll( "\\]\"", "]" );
JSONObject jObject = new JSONObject(jsonResultStr);
JSONArray jArray = jObject.optJSONArray("data");
答案 3 :(得分:0)
你的json应该是这样的
{
"myarray": [
{
"METER_READING": "15",
"UTILITY_PLAN": "1",
"uname": "vinayak@triffort.com",
"kwh_usage": "3",
"meter_reading_date": "02-13-2014",
"ESID": "abc",
"METER_ID": "abc100"
}
]
}
用于网络电话
public String initializeConnection(String url) {
String result = null;
JSONObject jObj;
try {
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
if(client==null){Log.i("Clinet **************** ", "Client is null");}
//post.setEntity(new UrlEncodedFormEntity(params));
HttpResponse res = client.execute(post);
result = inputStreamToString(res.getEntity().getContent()).toString();
Log.d("Result from server:", result);
jObj = new JSONObject(result.trim());
} catch (JSONException e1) {
Log.e("Json Exception", e1.toString());
} catch (ClientProtocolException e2) {
Log.e("Client Protocol", e2.toString());
} catch (IOException e3) {
Log.e("Io exception", e3.toString());
}
return result;
}
private StringBuilder inputStreamToString(InputStream is) throws UnsupportedEncodingException {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is, "iso-8859-1"),8);
try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
}
catch (IOException e) {
e.printStackTrace();
}
return answer;
}
从json中退出
ArrayList<String> params = new ArrayList<String>();
String result = networkCall.initializeConnection(url);
jObj = new JSONObject(result);
JSONArray jArray = jObj.optJSONArray("myarray");
params.add(jArray.optString(1));
params.add(jArray.optString(2));
params.add(jArray.optString(3));
params.add(jArray.optString(4));
params.add(jArray.optString(5));
params.add(jArray.optString(6));
现在数据存储在params
中,您可以区分&amp;根据需要存储
答案 4 :(得分:0)
你可以这样做:
JSONArray jsonArray = new JSONArray(result); // Pass your result here..
JSONObject jsonObject = jsonArray.getJSONObject(0);
String meterReading = jsonObject.getString("METER_READING");
String plan = jsonObject.getInt("UTILITY_PLAN");
String uname= jsonObject.getString("uname");
String meter_reading_date= jsonObject.getString("meter_reading_date");
String ESID= jsonObject.getString("ESID");
String METER_ID= jsonObject.getString("METER_ID");