如何从 Json 获取特定值? [英] how to get the particular value from Json?
本文介绍了如何从 Json 获取特定值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有如下所示的 Json 数据,我需要获取默认键值对为 1 的数据,然后我需要获取剩余的字典数据并需要传递给用户以显示可以任意有人告诉我如何实现这一点?
I am having the Json data as shown below in this I need to get the data that which key value pair for default is 1 then i need to get the remaining dictionaries data and need to be passed to the user to display can any one tell me how to implement this ?
我的代码如下所示
if let addressArray = jsonObj!.value(forKey: "address") as? NSArray{
for array in addressArray {
if let addressDict = array as? NSDictionary{
if let Default = addressDict.value(forKey: "default"){
}
}
}
}
"address": [
{
"default": 0,
"number": 9123456711,
"name": "Ramesh",
"address": "No:11/111 ,cross street,Nungambakkam,mylapore,chennai :600088"
},
{
"default": 1,
"number": 8123456722,
"name": "Vignesh",
"address": "No:22/222 ,cross street,Perambur,chennai :600012"
},
{
"default": 0,
"number": 7123456733,
"name": "Rajesh",
"address": "No:33/333 ,cross street,Villivakkam,chennai :600045"
}
]
推荐答案
您可以检查 Default 是否等于 1 并将这些值添加到数组中.
You can check if Default equals 1 and add those values to an array.
var defaultArray = [NSDictionary]()
if let addressArray = jsonObj!.value(forKey: "address") as? NSArray{
for array in addressArray {
if let addressDict = array as? NSDictionary{
if let Default = addressDict.value(forKey: "default"){
if Default == 1 {
defaultArray.append(addressDict)
}
}
}
}
}
这篇关于如何从 Json 获取特定值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文