1

I have some error not undestand.. Well i send this code

JSONARRAY {
 JSON,
 JSONARRAY,
 JSONARRAY
}

Y have this but not working or not parse correct, no matter for while the two array is not adding anything

buscarEmpresa=[

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

  ],
  [

  ]
]

The data couldn’t be read because it isn’t in the correct format.

try this code..

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)
3
  • The above code itself seems to work fine. Where do you get the error? Also, it's better to use Swift arrays and dictionaries with type information than NSArray and NSDictionary in Swift code ... Commented Mar 22, 2017 at 23:27
  • Ok understand but some tutorial or example, please ? Commented Mar 22, 2017 at 23:32
  • 1
    See the update to my answer - I added some valid JSON based on the output from your code. Commented Mar 22, 2017 at 23:39

2 Answers 2

1

The generated data is not valid JSON. That is probably why you get the error. You can verify this using a JSON validator, if you want to but depending on how you want to set things up, you might want this line of JSON:

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

To be represented as key value pairs inside your JSON object, or as a value for a key. Something like this would be valid:

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

You can use this JSON validator, to validate your own JSON.

Sign up to request clarification or add additional context in comments.

Comments

0

Well friends, I already solved it.. have changes

This not working for me, so make changes..

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)

This working fine for me

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)

And at the end I also made changes to my loops

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)

Thank you all for your answers i appreciate it a lot

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.