这是我的JSON响应:
[
[
{
"id": 22,
"request_id": "rqst5c12fc9e856ae1.06631647",
"business_name": "Code Viable",
"business_email": "code@viable.com",
"title": "Apache Load/Ubuntu",
}
],
[
{
"id": 24,
"request_id": "rqst5c130cae6f7609.41056231",
"business_name": "Code Viable",
"business_email": "code@viable.com",
"title": "Load",
}
]
]
此JSON结构在数组内部获得了一个数组,内部数组的对象正是我要解析的对象。这是我的映射器:
struct JobResponseDataObject: Mappable {
init?(map: Map) {
}
var id: Int?
var requestId: String?
var businessName: String?
var businessEmail: String?
mutating func mapping(map: Map) {
id <- map["id"]
requestId <- map["request_id"]
businessName <- map["business_name"]
businessEmail <- map["business_email"]
}
}
我尝试创建另一个映射器struct
来保存对象[JobResponseDataObject]
的数组,并对其使用Alamofire的responseArray,但是没有用。我也尝试过用0.
给我的json ID前缀,但这也没有用。请帮助
谢谢
答案 0 :(得分:3)
所以这很重要... Codable是Apple的一个很酷的协议,用于处理来自API的JSON响应解析。您得到的是一个数组数组,因此您的内容将如下所示:
public function Order(Request $request){
$orderDetail = OrderDetail::where(['food_id' => $request->input('food'), 'order_id' => $order->id])->get();
...
if(...){
$orderDetail1->quantity += 1;
$orderDetail1->save();
}
}
因此,无论如何,您将为对象创建一个结构,如下所示:
[[ResponseObject]]
您会注意到我稍微更改了键名(我使用struct ResponseObject: Codable {
let id: Int?
let requestId: String?
let businessName: String?
let businessEmail: String?
let title: String?
}
代替了request_id
)。原因是requestId
具有称为JSONDecoder
的属性,该属性提供了可以选择的固定解码策略的枚举。您会做keyDecodingStrategy
。
您可以在这里的代码转储到操场上进行修改。基本上,声明您的结构,使其与JSON中的键匹配,声明一个解码器,将其提供给解码策略,然后对其进行解码。
以下是您拨打Alamofire电话的方法:
convertFromSnakeCase
这里是您可以在操场上抓取的代码。
private let backgroundThread = DispatchQueue(label: "background",
qos: .userInitiated,
attributes: .concurrent,
autoreleaseFrequency: .inherit,
target: nil)
Alamofire.request(url).responseJSON(queue: backgroundThread) { (response) in
guard response.result.error == nil else {
print("KABOOM!")
return
}
if let data = response.data {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
do {
let parsedResponse = try decoder.decode([[ResponseObject]].self, from: data)
print(parsedResponse)
} catch {
print(error.localizedDescription)
}
}
}
答案 1 :(得分:0)
您应该将此JobResponseDataObject
结构用作[[JobResponseDataObject]]
而不是[JobResponseDataObject]
-在此处使用父struct或class中的该结构来创建属性。
答案 2 :(得分:-1)
您可以在此处使用// ==UserScript==
// @name Inject script
// @namespace myproject
// @version 0.1
// @description some description
// @author Admin
// @match https://somewebsite.com/*
// @run-at document-start
// ==/UserScript==
document.onreadystatechange = function () {
console.log(document.readyState);
if (document.readyState === "interactive") {
var modif2 = document.createElement("script");
modif2.type = "text/javascript";
modif2.src = 'https://unpkg.com/xhook@1.4.9/dist/xhook.min.js';
document.getElementsByTagName('head')[0].appendChild(modif2);
console.log('--- inserted src ----');
} else if (document.readyState === "complete") {
var modif = document.createElement("script");
modif.type = "text/javascript";
modif.innerHTML = `xhook.after(function(request, response) {
console.log('-----request accepted -----');
console.log(response.text)
});
`;
document.getElementsByTagName('head')[0].appendChild(modif);
console.log('--- injected JAVASCRIPT ----');
}
}
来映射Codable
响应,JSON
JobResponseDataObject
应该看起来像
struct