0

I need create a json for send from a API REST:

{
  "ownId": "seu_identificador_proprio",
  "amount": {
    "currency": "BRL",
    "subtotals": {
      "shipping": 1000
    }
  },
  "items": [
    {
      "product": "Descrição do pedido",
      "quantity": 1,
      "detail": "Mais info...",
      "price": 1000
    }
  ],
  "customer": {
    "ownId": "seu_identificador_proprio_de_cliente",
    "fullname": "Jose Silva",
    "email": "[email protected]",
    "birthDate": "1988-12-30",
    "taxDocument": {
      "type": "CPF",
      "number": "22222222222"
    },
    "phone": {
      "countryCode": "55",
      "areaCode": "11",
      "number": "66778899"
    },
    "shippingAddress": {
      "street": "Avenida Faria Lima",
      "streetNumber": 2927,
      "complement": 8,
      "district": "Itaim",
      "city": "Sao Paulo",
      "state": "SP",
      "country": "BRA",
      "zipCode": "01234000"
    }
  }
}

I am confused with the creation..

I try begin with [NSObject:AnyObject]

    var d1 : [NSObject:AnyObject] = ["ownId":"seu_identificador_proprio", "customer":""]
    let dd1 = ["currency":"BRL"]
    let dd2 = ["shipping":"1000"]
    let arr = [d1]
    let d = try! NSJSONSerialization.dataWithJSONObject(arr, options: NSJSONWritingOptions.PrettyPrinted)
    let s = NSString(data: d, encoding: NSUTF8StringEncoding)! as String
    print(s)

But I need help!

6
  • are you using swift 3? Commented Nov 22, 2016 at 13:56
  • No, swift 2.3 @dirtydanee Commented Nov 22, 2016 at 13:57
  • So what's your question? What's wrong with the code you posted? (BTW, there's no reason other than testing to convert your JSON output from NSData to a string, and also no reason to use pretty printing format. For sending to a RESTful server you should not use the pretty format.) Commented Nov 22, 2016 at 14:06
  • Your code looks good to me, what is the output for your print(s) ? Commented Nov 22, 2016 at 14:14
  • Look good to me. Also you can check SwiftyJSON too. Commented Nov 22, 2016 at 14:18

1 Answer 1

1

I have updated your code and added some hints, how can you build the above listed structure. Happy coding!

 // Do not use NSObject as key's type
 // Keys in a dictionary are usually Strig in every language
 var d1: [String: AnyObject] = ["ownId":"seu_identificador_proprio", "customer":""]

 // Define the type of your dictionaries, if you dont, in this case it will create a [String:String] dictionary, but you need to insert an array into it
 // Make it a var, so you can mutate the container
 var dd1: [String: AnyObject] = ["currency":"BRL"]
// Here the type is undefined. Try inserting anything else than String, and see the results
let dd2 = ["shipping":"1000"]
dd1["subtotals"] = dd2
d1["amount"] = dd1
// Build all your dictionaries like i did above, and at the end add all of them into arr
let arr = [d1]   
// Do not force try any throwing function in swift - if there is an error, your application will crash
// Use proper error handling - https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html

do {
 let d = try NSJSONSerialization.dataWithJSONObject(arr, options: NSJSONWritingOptions.PrettyPrinted)
 let s = NSString(data: d, encoding: NSUTF8StringEncoding)! as String
 print(s)
} catch {
// Do your error handling here
}
Sign up to request clarification or add additional context in comments.

2 Comments

I understand your code, now I will custom for my app, thanks brow, if I have other ask, I post here!
do not post it here! I am not sure, how will i have time. However, the community is huge, and i am sure somebody will pick up your question.

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.