Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

ios - how to get the particular value from Json?

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 ?

And my code is as shown below

            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"
    }
  ]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)
                              }
                            }
                        }
                    }
                }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...