Json Swift 3解析API响应故障
我试图解析Swift中的API响应,但在获取响应中的嵌套对象和数组时遇到问题 这是我的json示例Json Swift 3解析API响应故障,json,swift,dictionary,Json,Swift,Dictionary,我试图解析Swift中的API响应,但在获取响应中的嵌套对象和数组时遇到问题 这是我的json示例 { "Id": "10", "Name": "PV Prediction By Site", "Description": "", "Permalink": "", "Source_format": "JSON", "Internal_function_name": "get-meteo-by-site", "Additional_parameters": "Predi
{
"Id": "10",
"Name": "PV Prediction By Site",
"Description": "",
"Permalink": "",
"Source_format": "JSON",
"Internal_function_name": "get-meteo-by-site",
"Additional_parameters": "Prediction",
"Sites": null,
"Data": [
{
"UTCDateString": "2017-05-01T20:10:33Z",
"Value": [
{
"metadata": {
"name": "Beck_Hill",
"latitude": 46.26,
"longitude": -112.44,
"height": 1926,
"timezone_abbrevation": "MDT",
"utc_timeoffset": -6,
"modelrun_utc": "2017-05-01 12:00",
"modelrun_updatetime_utc": "2017-05-01 16:41",
"kwp": 40.26,
"slope": 30,
"facing": 180,
"tracking": 0
},
"units": {
"time": "YYYY-MM-DD hh:mm",
"pvpower": "kW",
"snowcover": "mm",
"iam": "percent",
"temperature": "C"
},
"data_xmin": {
"time": [
"2017-05-01 07:00",
"2017-05-01 07:15",
"2017-05-01 07:30",
"2017-05-01 07:45",
"2017-05-01 08:00",
"2017-05-01 08:15",
"2017-05-01 08:30"
],
"pvpower_instant": [
40.26,
40.26,
40.26,
40.26,
40.26
]
}
}
]
}
]
}
这是我的一些解析代码,我可以很好地获得“Data”数组中的第一个对象,但是当我试图获得值字符串中的第一个对象时,它无法将AnyObject转换为其他任何对象
//get a Dictionary of sites
sitesDictionary = try JSONSerialization.jsonObject(with: decodedData, options: .allowFragments) as? [[String:AnyObject]]
CoreDataStack.sharedInstance.persistentContainer.performBackgroundTask({ (context) in
//loop thorugh all site and create SiteMO objects from them
for site in (sitesDictionary?.enumerated())! {
//SiteMO
let siteMO = SiteMO.siteInfo(siteInfo: site.element, inManagedObjectContext: context)!
let siteFeedsDictionary = site.element["Feeds"] as! [[String:AnyObject]]
//loop through every feed object and create FeedMO objects from them
for feed in siteFeedsDictionary.enumerated() {
//FeedMO
let feedMO = FeedMO.feedInfo(feedInfo: feed.element, site: siteMO, inManagedObjectContext: context)!
//what type of data is in the feed?
switch feedMO.additionalParameters! {
case "Weather":
//its a feed with a Weather object
print("There should be a WeatherMO created Here")
case "Prediction":
//its a feed with a Prediction object
let dataArray = feed.element["Data"] as? [[String:AnyObject]]
我需要一些元数据“单位”和“数据”对象中的数据
结果[“数据”][0][“值”][0]“值”是一个数组,您试图将其作为字典解析,但当我这样做时,让Value=feedDataDictionary[“值”]?。firstObject作为?[String:AnyObject]当我使用上述[“数据”]时,我得到零[0]是否为Any?并且没有成员“值”