使用 swiftyjson 和 swift 解析嵌入的 json
parse embedded json using swiftyjson and swift
所以我试图使用 swiftyjson 从这个 json 文件中解析出这个嵌入值,但我不知道如何从 json 中获取嵌入的东西。
这是我目前所拥有的,它适用于取出 json 的上层,但不适用于嵌套项目。我需要从该文件中获取的主要内容是名称的项目部分和项目中创建的值。
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)
}
这是我要解析的 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
} ]
}
]
}
感谢您对此的任何帮助
MNM,
您可以
访问项目
var myItems = newJson["order_history"][0]["items"]
绝对没有必要为每个键创建单独的 json。
根据documentation。 .stringValue
return 如果值为 JSON 字符串类型,则为 String
。您不能将其用于 "convert it to String
from whatever type it currently has"。因此,如果它是一个 JSON 数组,.stringValue
将 return 一个 ""
.
如果要获取值的原始(未解析)JSON 字符串,请使用 .rawString()
。
对于你的情况,你可以这样做:
for item in newJson["items"].arrayValue {
// do something with the item
}
您正在使用 SwiftyJSON,所以请使用它的功能!
例如,使用键路径访问一个值:
var id = newJson["order_history",0,"items",0,"id"]
此外,经典的 SwiftyJSON 方式(类似于本机 Swift 但无需解包值):
var id = newJson["order_history"][0]["items"][0]["id"]
所以我试图使用 swiftyjson 从这个 json 文件中解析出这个嵌入值,但我不知道如何从 json 中获取嵌入的东西。
这是我目前所拥有的,它适用于取出 json 的上层,但不适用于嵌套项目。我需要从该文件中获取的主要内容是名称的项目部分和项目中创建的值。
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)
}
这是我要解析的 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
} ]
}
]
}
感谢您对此的任何帮助
MNM,
您可以
访问项目var myItems = newJson["order_history"][0]["items"]
绝对没有必要为每个键创建单独的 json。
根据documentation。 .stringValue
return 如果值为 JSON 字符串类型,则为 String
。您不能将其用于 "convert it to String
from whatever type it currently has"。因此,如果它是一个 JSON 数组,.stringValue
将 return 一个 ""
.
如果要获取值的原始(未解析)JSON 字符串,请使用 .rawString()
。
对于你的情况,你可以这样做:
for item in newJson["items"].arrayValue {
// do something with the item
}
您正在使用 SwiftyJSON,所以请使用它的功能!
例如,使用键路径访问一个值:
var id = newJson["order_history",0,"items",0,"id"]
此外,经典的 SwiftyJSON 方式(类似于本机 Swift 但无需解包值):
var id = newJson["order_history"][0]["items"][0]["id"]