我已经尝试了许多版本的解析来自我的 .net Web 服务的 JSON 数据,而我目前的版本在我的 JSON 数据前面添加了一个 {"d": ..... 我不知道为什么。
手动调用我的网络服务时,数据格式正确。但是,当我使用这个解析器时,我检查了使用 .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();
}