JSON 响应未映射到 JSON android 中的对象
JSON response not mapped to JSONObject in android
我正在尝试读取来自 android 应用程序的 GET JSON 响应,服务器是在 ASP .net
中实现的 WCF Web 服务
[OperationContract(Name = "Employee")]
[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedResponse, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "person/{name}")]
Person GetPersonData(string name);
这是浏览器的响应:
{"EmployeeResult":{"Age":31,"Name":"testuser"}}
- 我怎样才能使它成为:
{"Employee":{"Age":31,"Name":"testuser"}}
我尝试使用下面的代码在 android 应用中提取年龄和姓名
JSONObject jsonRootObject = new JSONObject(jsonStr);
JSONArray jsonArray = jsonRootObject.optJSONArray("EmployeeResult");
for(int i=0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
int id = Integer.parseInt(jsonObject.optString("Age").toString());
String name = jsonObject.optString("Name").toString();
Log.i("JsonClient", "Age: "+id+" Name: "+name);
}
jsonArray 为空,为什么? JSON 格式不正确? jsonRootObject 不为空且 jsonStr 具有正确的响应文本
谢谢
EmployeeResult
不是 JSONArray
,而是 JSONObject
。
改为
JSONObject jsonRootObject = new JSONObject(jsonStr);
JSONObject employeeResult = jsonObject.getJSONObject("EmployeeResult");
String name = employeeResult.getString("Name");
int age = employeeResult.getInt("Age");
嗯EmployeeResult不是JsonArray而是JsonObject
在下方粘贴您的 json 回复 link
JSONObject jsonRootObject = new JSONObject(jsonStr);
JSONObject EmployeeResultJsonObject = jsonRootObject.getJSONObject("EmployeeResult");
int id = EmployeeResultJsonObject.getInt("age");
String name = EmployeeResultJsonObject.getString("Name");
Log.i("JsonClient", "Age: "+id+" Name: "+name);
我正在尝试读取来自 android 应用程序的 GET JSON 响应,服务器是在 ASP .net
中实现的 WCF Web 服务[OperationContract(Name = "Employee")]
[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedResponse, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "person/{name}")]
Person GetPersonData(string name);
这是浏览器的响应:
{"EmployeeResult":{"Age":31,"Name":"testuser"}}
- 我怎样才能使它成为:
{"Employee":{"Age":31,"Name":"testuser"}}
我尝试使用下面的代码在 android 应用中提取年龄和姓名
JSONObject jsonRootObject = new JSONObject(jsonStr); JSONArray jsonArray = jsonRootObject.optJSONArray("EmployeeResult"); for(int i=0; i < jsonArray.length(); i++){ JSONObject jsonObject = jsonArray.getJSONObject(i); int id = Integer.parseInt(jsonObject.optString("Age").toString()); String name = jsonObject.optString("Name").toString(); Log.i("JsonClient", "Age: "+id+" Name: "+name); }
jsonArray 为空,为什么? JSON 格式不正确? jsonRootObject 不为空且 jsonStr 具有正确的响应文本
谢谢
EmployeeResult
不是 JSONArray
,而是 JSONObject
。
改为
JSONObject jsonRootObject = new JSONObject(jsonStr);
JSONObject employeeResult = jsonObject.getJSONObject("EmployeeResult");
String name = employeeResult.getString("Name");
int age = employeeResult.getInt("Age");
嗯EmployeeResult不是JsonArray而是JsonObject
在下方粘贴您的 json 回复 link
JSONObject jsonRootObject = new JSONObject(jsonStr);
JSONObject EmployeeResultJsonObject = jsonRootObject.getJSONObject("EmployeeResult");
int id = EmployeeResultJsonObject.getInt("age");
String name = EmployeeResultJsonObject.getString("Name");
Log.i("JsonClient", "Age: "+id+" Name: "+name);