我试图在我的示例iOS应用程序中调用Bittrex api。
我试图从这里读取JSON。 https://bittrex.com/api/v1.1/public/getmarketsummaries
但是我收到了这个错误:预计会解码数组,但会找到一个字典。",underlyingError:nil)
根据Google搜索结果,JSON Struct不正确。
我在哪里犯了错误?
这是我的JSON Struct;
struct MarketSummaries : Decodable{
let success : Bool?
let message : String?
let result : [SummaryResult]?
}
struct SummaryResult : Decodable{
let marketName : String?
let high : Double?
let low : Double?
let volume : Double?
let last : Double?
let baseVolume : Double?
let timeStamp : String?
let bid : Double?
let ask : Double?
let openBuyOrders : Int?
let openSellOrders : Int?
let prevDay : Double?
let created : String?
private enum CodingKeys : String, CodingKey {
case marketName = "MarketName", high = "High", low = "Low", volume = "Volume",
last = "Last", baseVolume = "BaseVolume", timeStamp = "TimeStamp", bid = "Bid",
ask = "Ask", openBuyOrders = "OpenBuyOrders", openSellOrders = "OpenSellOrders",
prevDay = "PrevDay", created = "Created"
}
}
这是我的JSON Struct;
let url = URL(string: "https://bittrex.com/api/v1.1/public/getmarketsummaries")
let session = URLSession.shared
let task = session.dataTask(with: url!) { (data, response, error) in
if error != nil {}
else
{
if (data != nil)
{
do
{
let coins = try JSONDecoder().decode([MarketSummaries].self, from: data!)
DispatchQueue.main.async {
self.market = coins
self.table.reloadData()
}
}
catch
{
print(error)
}
}
}
}
task.resume()
答案 0 :(得分:1)
我发现了自己的错误。我从JSON读取数据时读取了像数组一样的MarketSummaries。但它不是阵列。
坏线:
layout.html
更正后的行
let coins = try JSONDecoder().decode([MarketSummaries].self, from: data!)
答案 1 :(得分:0)
您可以使用此Result类来检索您的Json数据......我想这会帮助您。
import foundation
public class Result {
public var marketName : String?
public var high : Double?
public var low : Double?
public var volume : Double?
public var last : Double?
public var baseVolume : Double?
public var timeStamp : String?
public var bid : Double?
public var ask : Double?
public var openBuyOrders : Int?
public var openSellOrders : Int?
public var prevDay : Double?
public var created : String?
/** Returns an array of models based on given dictionary.
Sample usage:
let result_list = Result.modelsFromDictionaryArray(someDictionaryArrayFromJSON)
- parameter array: NSArray from JSON dictionary.
- returns: Array of Result Instances.
*/
public class func modelsFromDictionaryArray(array:NSArray) -> [Result]
{
var models:[Result] = []
for item in array
{
models.append(Result(dictionary: item as! NSDictionary)!)
}
return models
}
/**
Constructs the object based on the given dictionary.
Sample usage:
let result = Result(someDictionaryFromJSON)
- parameter dictionary: NSDictionary from JSON.
- returns: Result Instance.
*/
required public init?(dictionary: NSDictionary) {
marketName = dictionary["MarketName"] as? String
high = dictionary["High"] as? Double
low = dictionary["Low"] as? Double
volume = dictionary["Volume"] as? Double
last = dictionary["Last"] as? Double
baseVolume = dictionary["BaseVolume"] as? Double
timeStamp = dictionary["TimeStamp"] as? String
bid = dictionary["Bid"] as? Double
ask = dictionary["Ask"] as? Double
openBuyOrders = dictionary["OpenBuyOrders"] as? Int
openSellOrders = dictionary["OpenSellOrders"] as? Int
prevDay = dictionary["PrevDay"] as? Double
created = dictionary["Created"] as? String
}
/**
Returns the dictionary representation for the current instance.
- returns: NSDictionary.
*/
public func dictionaryRepresentation() -> NSDictionary {
let dictionary = NSMutableDictionary()
dictionary.setValue(self.marketName, forKey: "MarketName")
dictionary.setValue(self.high, forKey: "High")
dictionary.setValue(self.low, forKey: "Low")
dictionary.setValue(self.volume, forKey: "Volume")
dictionary.setValue(self.last, forKey: "Last")
dictionary.setValue(self.baseVolume, forKey: "BaseVolume")
dictionary.setValue(self.timeStamp, forKey: "TimeStamp")
dictionary.setValue(self.bid, forKey: "Bid")
dictionary.setValue(self.ask, forKey: "Ask")
dictionary.setValue(self.openBuyOrders, forKey: "OpenBuyOrders")
dictionary.setValue(self.openSellOrders, forKey: "OpenSellOrders")
dictionary.setValue(self.prevDay, forKey: "PrevDay")
dictionary.setValue(self.created, forKey: "Created")
return dictionary
}
}