简体   繁体   English

使用swiftyjson和swift解析嵌入式json

[英]parse embedded json using swiftyjson and swift

So I am trying to parse out this embedded values from this json file using swiftyjson but I cannot figure out how to get the embedded stuff out of the json. 所以我试图使用swiftyjson从这个json文件中解析出这个嵌入值,但我无法弄清楚如何从json中获取嵌入的东西。

This is what I have so far and it works for getting the upper levels of json out but not for the nested items. 这是我到目前为止所做的,它适用于获取json的上层,但不适用于嵌套项。 The main thing that I need out of this file is the items section for the name and the created values in the items. 我需要的主要内容是名称的项目部分和项目中创建的值。

  if let data = json.dataUsingEncoding(NSUTF8StringEncoding) {
        let newJson = JSON(data: data)
        myBarcode = newJson["barcode_id"].stringValue
        myName = newJson["name"].stringValue
        myTotalPointsEarned = newJson["total_points_earned"].stringValue
        myPointsEarned = newJson["points_available_to_spend"].stringValue
        myRank = newJson["rank"].stringValue
        myId = newJson["id"].stringValue
        //new json arrary to get the items and dates
        var myHistory = newJson["order_history"].arrayValue
        print("\n My Hist \n" , myHistory)

        //FAIL
        //var myItems = newJson["items"].stringValue
        //print("\n My Items \n" , myItems)
    }

And this is the json file that I am trying to parse 这是我试图解析的json文件

{
 "id" : "xxx",
 "name" : "xfgsfsdfs",
 "total_points_earned" : null,
 "points_available_to_spend" : null,
 "rank" : null,
 "barcode_id" : "C-00000252",
 "order_history" : [ {
    "items" : [ {
       "id" : 284,
       "created" : [ 2016, 5, 26, 5, 27, 53 ],
       "updated" : [ 2016, 5, 26, 5, 27, 53 ],
       "sku" : "10-10-08-050",
       "name" : "Halloween stuff",
       "description" : "",
  "quantity" : 1.0,
  "price" : 2000.0,
  "total" : 2000.0,
  "tax" : null,
  "discount" : null
}, {
  "id" : 285,
  "created" : [ 2016, 5, 26, 5, 27, 53 ],
  "updated" : [ 2016, 5, 26, 5, 27, 53 ],
  "sku" : "10-22-12-247",
  "name" : "More Xmas stuff",
  "description" : "",
  "quantity" : 1.0,
  "price" : 2300.0,
  "total" : 2300.0,
  "tax" : null,
  "discount" : null
}, {
  "id" : 286,
  "created" : [ 2016, 5, 26, 5, 27, 53 ],
  "updated" : [ 2016, 5, 26, 5, 27, 53 ],
  "sku" : "10-22-12-249",
  "name" : "Xmas stuff",
  "description" : "",
  "quantity" : 1.0,
  "price" : 3700.0,
  "total" : 3700.0,
  "tax" : null,
  "discount" : null
} ],
"items" : [ {
  "id" : 288,
  "created" : [ 2016, 5, 26, 5, 29, 51 ],
  "updated" : [ 2016, 5, 26, 5, 29, 51 ],
  "sku" : "JJ-02-00-042",
  "name" : "A sample product name",
  "description" : "",
  "quantity" : 1.0,
  "price" : 3000.0,
  "total" : 3000.0,
  "tax" : null,
  "discount" : null
} ]

 }
 ]
 }

Thanks for any help with this 感谢您的帮助

MNM, MNM,

You can access items as 您可以访问项目

var myItems = newJson["order_history"][0]["items"]

There is absolutely no need to create seperate json for each keys. 绝对不需要为每个键创建单独的json。

You are using SwiftyJSON, so use its features! 您正在使用SwiftyJSON,因此请使用其功能!

For example, accessing a value with key path: 例如,使用键路径访问值:

var id = newJson["order_history",0,"items",0,"id"]

Also, the classic SwiftyJSON way (similar to native Swift but without having to unwrap the values): 此外,经典的SwiftyJSON方式(类似于原生的Swift,但无需打开值):

var id = newJson["order_history"][0]["items"][0]["id"]

According to the documentation . 根据文件 .stringValue returns a String if the value is of JSON string type. 如果值为JSON字符串类型,则.stringValue返回String You cannot use it to "convert it to String from whatever type it currently has". 你不能用它“将它从目前的任何类型转换为String ”。 So if it's a JSON array, .stringValue will return a "" . 因此,如果它是一个JSON数组, .stringValue将返回一个""

If you want to get the raw (unparsed) JSON string of the value, use .rawString() . 如果要获取值的原始(未解析)JSON字符串,请使用.rawString()

In your case, you could just do: 在你的情况下,你可以这样做:

for item in newJson["items"].arrayValue {
  // do something with the item
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM