swift:不能下载类型为[Dictionary <string,anyobject =“”>]的值,其索引类型为string

时间:2016-10-17 12:06:43

标签: ios json swift3

我正在尝试在swift 3中进行json解析。我得到了上面提到的错误。我的解析技术如下:

if let responseData = data {
   do {
     let json = try JSONSerialization.jsonObject(with: responseData, options: JSONSerialization.ReadingOptions.allowFragments)
     if let dict = json as? [Dictionary<String, AnyObject>] {
        if let localityName = dict["name"] as? String ,let localityId = dict["_id"] as? String {

         }               
     }           
   } catch {
       print("could not serialize")            
   }
}

我收到了错误:

if let localityName = dict["name"] as? String ,let localityId = dict["_id"] as? String

请告诉我如何解决此问题

2 个答案:

答案 0 :(得分:1)

<div> <h3>Selected Name:</h3> <h3>{{person.name}}</h3> </div> 属性是一个字典数组,而不是字典。 您可以访问以下第一个元素:

dict

答案 1 :(得分:0)

<div class="col-lg-6"> <div class="form-group"> <label class="control-label">{{controls.label}}</label> <input type="checkbox" class="form-control input-lg mandatory" ng-model="formData[$parent.$parent.$index][controls.id]" value="{{controls.value}}" name="control_{{$parent.$parent.$index}}_{{controls.id}}" ng-required="{{controls.mandatory}}"> <div ng-show="submitted && profilecreate.control_{{$parent.$parent.$index}}_{{controls.id}}.$error.required" class="error_message">This field is required</div> </div> </div> <div class="col-lg-6" ng-repeat="child in controls.children"> <div class="form-group" ng-hide="!formData[$parent.$parent.$index][child.parentId]"> <label class="control-label">{{child.label}}</label> <input type="{{child.type}}" id="{{$parent.$parent.$index}}_{{child.id}}" ng-model="formData[$parent.$parent.$index][child.id]" name="control_{{$parent.$parent.$index}}_{{child.id}}" ng-disabled="!formData[$parent.$parent.$index][child.parentId]" ng-required="{{child.mandatory}}" ng-class="!formData[$parent.$parent.$index][child.parentId] ? 'disabled' : 'normalinput'"> <div ng-show="submitted && profilecreate.control_{{$parent.$parent.$index}}_{{child.id}}.$error.required" class="error_message">This field is required</div> </div> </div> 实际上是dict。创建一个循环来迭代它

arr