我尝试过很多版本的解析来自我的.net网络服务和我当前的JSON数据 在我的JSON数据前添加了一个{“d”:.....我不知道为什么。
手动调用我的webservice时,数据格式正确。 但是当我使用这个解析器时,我已经检查过.names()以列出JSONObjects的名称,它只显示[“d”]
是否有任何推荐的代码来解析java中的JSON Web服务?
继承我解析数据的代码
//initialize
InputStream is = null;
String result = "";
JSONObject jArray = null;
String url = "http://10.0.2.2:1672/Eventurous/WsEventurousMobile.asmx/getEventsList";
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
JSONObject obj = new JSONObject();
obj.put("category",category);
httppost.setEntity(new StringEntity(obj.toString(),"UTF-8"));
httppost.setHeader("Accept","application/json");
httppost.setHeader("Content-type","application/json");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//try parse the string to a JSON object
try{
jArray = new JSONObject(result);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
try{
JSONArray catalogObj = jArray.getJSONArray("Table");
//Im using the newtonsoft json dataset converter and so the name of the JSONArray
//is by default Table and i have no idea how to change it either
JSONObject event = catalogObj.getJSONObject(0);
return event.getString("EventName");
}
catch(Exception e)
{
return e.toString();
}
答案 0 :(得分:0)
我建议使用gson lib,http://code.google.com/p/google-gson/。
是否有任何推荐的代码来解析java中的JSON Web服务?
在不知道json数据的情况下,很难给出任何具体的建议。但无论如何,我建议使用gson lib,这在Android开发者中很受欢迎。