我无法解析json中的某个值

时间:2018-08-03 08:44:51

标签: ios json swift

我写了一个解析器,为我提供了所需的ID,但是现在JSON响应的视图已更改,解析器不起作用,我现在不明白如何获取此ID 我有此代码:

  \"jsonrpc\":\"2.0\",
  \"result\":{
  \"s\":[
 {
  \"n\":\"@Лицо\",
  \"t\":\"Идентификатор\"
 },
 {
  \"n\":\"Фамилия\",
  \"t\":\"Строка\"
 },
 {
  \"n\":\"Имя\",
  \"t\":\"Строка\"
 },
 {
  \"n\":\"Отчество\",
  \"t\":\"Строка\"
 },
 {
  \"n\":\"Подразделение\",
  \"t\":\"Строка\"
 },
 {
  \"n\":\"ФИО\",
  \"t\":\"Строка\"
 },
 {
  \"n\":\"ИдПодразделения\",
  \"t\":\"Число целое\"
 },
 {
  \"n\":\"ФотоМини\",
  \"t\":\"Число целое\"
 },
 {
  \"n\":\"ФотоБольшое\",
  \"t\":\"Число целое\"
 },
 {
  \"n\":\"ИдДолжности\",
  \"t\":\"Число целое\"
 }
  ],
  \"f\":0,
  \"d\":[
  [
  22151885
  ],
  \"Прёсто\",
  \"Руководитель\",
  \"Предприятия\",
  \"Бухгалтерия\",
  \"Прёсто Руководитель Предприятия\",
  23077965,
  64349,
  64685,
  3051
  ],
  \"_type\":\"record\"
  },
  \"id\":1,
  \"protocol\":5
  }

我正在尝试获取值“ @Лицо”

这是我的解析器

struct Profile { 

    var ID: String? 

    var surname: String? 

} 

class ProfileBuilder : Builder { 

    typealias Element = Profile 

    var profile: Profile? 

    func setValue(property: String, value: Any) -> Self { 

        switch property { 

        case "@Лицо": 

            profile!.ID = value as? String 

        default: 

            // do nothing 

            break 

        } 

        return self 

    } 

    func build() -> Profile { 

        return profile! 

    } 



    func reset() { 

        profile = Profile() 

    } 

} 



 func parseRecord<B: Builder>(result: [String: Any], builder: B) throws -> B.Element { 

    let type = result["_type"] as! String 

    if(type == "record") { 

            let s = result["s"] as! [[String: Any]] 

            let d = result["d"] as! [Any] 

            builder.reset() 

            for (index, property) in s.enumerated() { 

            let propertyName = property["n"] as! String 

            builder.setValue(property: propertyName, value: d[index]) 

        } 

        return builder.build() 

        } 



            else { 

                throw RuntimeError("Error deserializing JSON: Json is not Record type") 



        } 

    } 

let profile = self.parseResponse(data: dataProfile!) { (result) in 

                return try self.parseRecord(result: result, builder: ProfileBuilder()) 

            } 

            switch profile { 

            case .Result(let profile): 



                print(profile.ID) 

              //  self.PersonalProfile = profile.ID 

                   print(self.PersonalProfile) 





            case .Error(let error): 



                print(error) 



                } 

            return 

            } 

解析器不起作用。也许值得在解析器中再添加一个条件。请帮助并解释我在做什么错

1 个答案:

答案 0 :(得分:0)

我认为问题出在ProfileBuilder的这一部分:

case "@Лицо": 
    profile!.ID = value as? String

在示例json中,@Лицо的值是一个整数数组,因此value as? String将失败。由于缺少某些部分,我无法运行您的解析器,因此您可以尝试以下操作:

case "@Лицо":
    profile!.ID = String((value as? [Int]).first!)

让我知道它是否有效。如果不是,请尝试提供一些更具描述性的错误消息。