为 API REST 创建 Json

Posted

技术标签:

【中文标题】为 API REST 创建 Json【英文标题】:Swift Create Json for API REST 【发布时间】:2016-11-22 13:45:42 【问题描述】:

我需要创建一个用于从 API REST 发送的 json:


  "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": "nome@email.com",
    "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"
    
  

我对创作感到困惑..

我尝试以[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)

但我需要帮助!

【问题讨论】:

你用的是swift 3吗? 不,swift 2.3 @dirtydanee 那么你的问题是什么?您发布的代码有什么问题? (顺便说一句,除了测试将您的 JSON 输出从 NSData 转换为字符串之外,没有其他理由,也没有理由使用漂亮的打印格式。要发送到 RESTful 服务器,您不应该使用漂亮的格式。) 您的代码在我看来不错,您的打印输出是什么? 对我很好。您也可以查看SwiftyJSON too. 【参考方案1】:

我已经更新了您的代码并添加了一些提示,您如何构建上面列出的结构。编码愉快!

 // 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

【讨论】:

我了解你的代码,现在我将为我的应用程序自定义,谢谢,如果我有其他问题,我在这里发布! 请勿在此处发布!我不确定,我怎么会有时间。然而,社区很大,我相信有人会回答你的问题。

以上是关于为 API REST 创建 Json的主要内容,如果未能解决你的问题,请参考以下文章

PayPal REST API 创建协议:传入 JSON 请求未映射到 API 请求

使用 JSON 服务器创建 REST API 的问题

ALM 使用 JSON 有效负载 (REST API) 创建新的测试运行

如何在使用 REST API 创建 Kafka 连接器时定义模式

如何使用 REST API 从服务帐户凭据创建访问令牌?

如果输出json具有带点(。)字符的键,如何在spring中为rest api创建域类