我从ASP.NET Web API返回JSON。 Json确实返回没有问题,但在Json数组的开头,我没有在android应用程序中使用的对象名称。
无论如何设置它?
这里是正在返回的Json
对象名称应该在开始。不仅仅是以ID开头
[{"ID":1197,"CustomerGuid":"ea0af124-ab88-45d8-88f7-3f122ef53c04","UserTrackingID":"2624","EmailAddress":"m.mashoodtariq@gmail.com","Password":"Pakistan99","IsBusiness":null,"FullName":"Mashood","DateRegistered":"2014-11-26T18:51:47.977","PostCode":"44000","BusinessName":null,"FactualID":null,"FacebookToken":null,"Country":null},{"ID":1202,"CustomerGuid":"30c3781e-506c-490a-a27d-03a29696bdcd","UserTrackingID":"2643","EmailAddress":"customer@hotmail.com","Password":"george69","IsBusiness":null,"FullName":"Pending Customer","DateRegistered":"2014-11-27T08:08:04.29","PostCode":"4220","BusinessName":null,"FactualID":null,"FacebookToken":null,"Country":null},{"ID":1203,"CustomerGuid":"cf7d09e4-2e84-4b16-b38c-d32579d1c6c4","UserTrackingID":"2646","EmailAddress":"tesr@hotmail.com","Password":"george69","IsBusiness":true,"FullName":"tester","DateRegistered":"2014-11-28T00:12:40.523","PostCode":"4102","BusinessName":"tester","FactualID":null,"FacebookToken":null,"Country":null}]
Public Class UsersController
Inherits System.Web.Http.ApiController
Private db As New PushStateNovember7Entities
' GET: api/Users
Function GetUsers() As IQueryable(Of User)
Return db.Users
End Function
End Class
您还可以直观地查看项目并访问helppage @
答案 0 :(得分:1)
不确定为什么一开始就需要一个名字。如果你只需要解析Android中的Json数组,你可以这样做:
假设您有以下用户类:
public class User {
private string _ID;
private string _CustomerGuid;
//and so on
public string setID(string Id){ _ID = Id;}
public string getID(){ return _ID;}
public string setCustomerGuid(string customerGuid){ _CustomerGuid = customerGuid;}
public string getCustomerGuid(){ return _CustomerGuid;}
}
要获取/解析Json数组,您可以执行以下操作:
JSONArray array = new JSONArray(yourApiReturnedJsonStringHere);
ArrayList<User> users = new ArrayList<User>();
for (int i=0; i < array.length(); i++) {
User user = new User();
JSONObject jObj = array.getJSONObject(i);
user.setID(jObj.getString("ID"));
user.setCustomerGuid(jObj.getString("CustomerGuid"));
//.....
users.add(user);
}
答案 1 :(得分:0)
你可以试试这个
var response = new ApiResponse<T>();
response.Data = db.Users;
return response;
这会将您的列表封装在数据名称
中如果您不使用API控制器,则只需创建Response类和
public Response<T> : where T : class
{
public T Data {get; set;}
}
return new Response<IList<Users>>() { Data = db.Users};
答案 2 :(得分:0)
大家好,谢谢你的回复,我也发现这篇帖子确切地解决了我的问题..