【发布时间】:2019-03-14 22:55:51
【问题描述】:
我是 iOS 编程的初学者,但我在 android 中工作,所以现在我在 iOS 中遇到了一个问题。 我知道我的问题很笼统,但我真的需要你的帮助! 在 android 中连接到服务器,我们如下所示:
Call<String> myList = service.Contact_List("");
myList.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
try{
ArrayList<Contact> contactArrayList = new ArrayList<>();
JSONArray jsonArray = new JSONArray(response.body());
for(int i=0 ; i<jsonArray.length() ; i++)
contactArrayList.add(gson.fromJson(jsonArray.getJSONObject(i).toString(), Contact.class));
}catch (Exception e) {
Log.d("Catch","Error")
}finally {
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Log.d("Failure","Error")
}
});
在 xcode 中我喜欢下面:
let url = URL(string: "http://api.example.com/Contact-List")
Alamofire.request(url!, method: HTTPMethod.post, parameters: param, encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
print("response.request")
print(response.request as Any) // original URL request
print("response.response")
print(response.response as Any) // URL response
print("response.result.value")
}
我的结果是这样的:
[{"Id":1,"Name":"Mary","TelNumber":"09111111"},{"Id":2,"Name":"Sarah","TelNumber":"09222222"},
{"Id":3,"Name":"Ben","TelNumber":"09333333"}]
现在我的问题是,我怎样才能在 Xcode(swift 3) 中像这样的代码:
for(int i=0 ; i<jsonArray.length() ; i++)
contactArrayList.add(gson.fromJson(jsonArray.getJSONObject(i).toString(), Contact.class));
另外,我在服务器端使用 ASP.net。
对于我的冗长而模棱两可的问题,我真的很抱歉! 感谢您的任何建议。
【问题讨论】:
标签: json xcode swift3 alamofire