alibaba / alibaba/HandyJSON

mappingArray失败,求解惑

Open
#190 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Swift
Stars
4.3k
Forks
677
PR merge metrics
No merged PRs in 30d

Description

尝试JsonString mapping modelArray 的时候失败,求解惑。。

### testModel code(export from jsonData)
```swift
struct TestModel : HandyJSON{

var result : Result?
var success : String?


}

struct Result : HandyJSON{

var disline : String?
var lists : List?
var page : String?
var totline : String?

}

struct List : HandyJSON{

var sh600000 : Sh600000?

}

struct Sh600000 : HandyJSON{

var buy1M : String?
var buy1N : String?
}
```
### defind func code (extension moya.response)
```swift
public func mapArray(_ type: T.Type ,path:String) throws -> [T] {

guard let ary = [T].deserialize(from: try mapString(), designatedPath:path) else {
throw MoyaError.jsonMapping(self)
}
return ary.flatMap{ $0.flatMap { $0 }}
}
```
### call func code (by moya)
```swift
TestApiProvider.request(.test) { Result in

switch Result {
case let .success(response):
var model:[List] = [List]()
do {

model = try response.mapArray(List.self, path: "result.lists")
print(model as Any)
} catch {

}
break
case .failure(_):
break
}
}
```
结果是调用时总是[T].deserialize转换失败(mapJsonStr是成功),抛出异常。
对Swift和HandyJSON还不是很了解,是我的用法错了吗?

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at the response.mapArray(List.self, path: "result.lists") call and compare the supplied TestModel, Result, List, and Sh600000 structures with the JSON passed to mapString(). Reproduce the failure and inspect the designated path and deserialization result; done means the cause is identified and the example either maps successfully or is clearly documented as unsupported.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.