JSON解析错误Swift 3

时间:2017-03-22 23:15:20

标签: ios json swift swift3

我有一些不错的错误.. 好吧,我发送此代码

JSONARRAY {
 JSON,
 JSONARRAY,
 JSONARRAY
}

Y有这个但不工作或不解析正确,无论两个数组没有添加任何东西

buscarEmpresa=[

"{\n  \"criterio\" : \"fru\",\n  \"tipo\" : \"0\",\n  \"idempresa\" : \"82\"\n}",
  [

  ],
  [

  ]
]

无法读取数据,因为格式不正确。

试试这段代码..

let usuario = UsuarioSQL.getUsuario()!
var jArray: [AnyObject] = []

let jUsuario = NSMutableDictionary()
jUsuario.setValue((phBuscar.text?.trimmingCharacters(in: .whitespacesAndNewlines))!, forKey: "criterio")
jUsuario.setValue(String(describing: usuario.tipo_negocio), forKey: "tipo")
jUsuario.setValue(String(describing: usuario.id_server), forKey: "idempresa")
let jData = try! JSONSerialization.data(withJSONObject: jUsuario, options: .prettyPrinted)
let jStr = NSString(data: jData, encoding: String.Encoding.utf8.rawValue) as! String
jArray.append(jStr as AnyObject)

let jaEmpresarial = NSMutableArray()
for i in receive_empresarial {
            let jEmpresarial = NSMutableDictionary()
            jEmpresarial.setValue(i, forKey: "empresarial")
            jaEmpresarial.add(jEmpresarial)
}
jArray.append(jaEmpresarial)

let jaPais = NSMutableArray()
for i in receive_paises {
      let jPais = NSMutableDictionary()
      jPais.setValue(i, forKey: "pais")
      jaPais.add(jPais)
 }
 jArray.append(jaPais)


 let post = try! JSONSerialization.data(withJSONObject: jArray, options: .prettyPrinted)
 let jPost = NSString(data: post, encoding: String.Encoding.utf8.rawValue) as! String

 let sesion = URLSession.shared
 let parameters = "buscarEmpresa=\(jPost)"
 print(parameters)

2 个答案:

答案 0 :(得分:1)

生成的数据无效JSON。这可能是你得到错误的原因。您可以使用JSON验证器验证这一点,如果您愿意,但根据您的设置方式,您可能需要这一行JSON:

{\n  \"criterio\" : \"some criteria\",\n  \"tipo\" : \"Type\",\n  \"idempresa\" : \"ID\"\n}

在JSON对象中表示为键值对,或作为键的值表示。这样的东西是有效的:

{
    "criterio": "some criteria",
    "tipo": "Type",
    "idempresa": "ID",
    "empresarial": [{
        "empresarial": 1
    }, {
        "empresarial": 2
    }, {
        "empresarial": 3
    }, {
        "empresarial": 4
    }],
    "pais": [{
        "pais": 10
    }, {
        "pais": 20
    }, {
        "pais": 30
    }]
}

您可以使用此JSON validator来验证您自己的JSON。

答案 1 :(得分:0)

好朋友,我已经解决了..有变化

这不适合我,所以要做出改变..

let usuario = UsuarioSQL.getUsuario()!
var jArray: [AnyObject] = []
    let jUsuario = NSMutableDictionary()
    jUsuario.setValue((phBuscar.text?.trimmingCharacters(in: .whitespacesAndNewlines))!, forKey: "criterio")
    jUsuario.setValue(String(describing: usuario.tipo_negocio), forKey: "tipo")
    jUsuario.setValue(String(describing: usuario.id_server), forKey: "idempresa")
    let jData = try! JSONSerialization.data(withJSONObject: jUsuario, options: .prettyPrinted)
    let jStr = NSString(data: jData, encoding: String.Encoding.utf8.rawValue) as! String
    jArray.append(jStr as AnyObject)

这对我来说很好

let usuario = UsuarioSQL.getUsuario()!
        var jArray: [AnyObject] = []
        let jUser: NSMutableDictionary = [
            "criterio" : String(describing: phBuscar.text!.trimmingCharacters(in: .whitespacesAndNewlines)),
            "tipo" : String(usuario.tipo_negocio),
            "idempresa" : String(usuario.id_server)
        ]
        jArray.append(jUser)

最后我也改变了我的循环

let usuario = UsuarioSQL.getUsuario()!
    var jArray: [AnyObject] = []
    let jUser: NSMutableDictionary = [
        "criterio" : String(describing: phBuscar.text!.trimmingCharacters(in: .whitespacesAndNewlines)),
        "tipo" : String(usuario.tipo_negocio),
        "idempresa" : String(usuario.id_server)
    ]
    jArray.append(jUser)

    let jaEmpresarial = NSMutableArray()
    for i in receive_empresarial {
        let jEmpresarial: NSMutableDictionary = [
            "empresarial" : i
        ]
        jaEmpresarial.add(jEmpresarial)
    }
    jArray.append(jaEmpresarial)

    let jaPais = NSMutableArray()
    for i in receive_paises {
        let jPais:NSMutableDictionary = [
            "pais" : i
        ]
        jaPais.add(jPais)
    }
    jArray.append(jaPais)

    let post = try! JSONSerialization.data(withJSONObject: jArray, options: .prettyPrinted)
    let jPost = NSString(data: post, encoding: String.Encoding.utf8.rawValue) as! String

    let sesion = URLSession.shared
    let parameters = "buscarEmpresa=\(jPost)"
    print(parameters)

谢谢大家的回答我非常感激