I have a problem parsing the array of objects from the JSON result.
[
{
"first_name":"vijay",
"last_name":"last",
"creditCardNumber":"178978977779787979",
"month":"02","year":"2012",
"address":"Addres2"
}
{
"first_name":"vijay",
"last_name":"last",
"creditCardNumber":"178978977779787979",
"month":"02","year":"2012",
"address":"Addres2"
}
{
"first_name":"vijay",
"last_name":"last",
"creditCardNumber":"178978977779787979",
"month":"02","year":"2012",
"address":"Addres2"
}
]
I wish to extract creditCardNumber
value from all objects in the array.
JSON parsing in Swift is a common thing to do. Almost every app decodes JSON to show data in a visualized way. Parsing JSON is definitely one of the basics you should learn as an iOS developer. Decoding JSON in Swift is quite easy and does not require any external dependencies.
Use the JSON. parse() method to pase a JSON array, e.g. JSON. parse(arr) . The method parses a JSON string and returns its JavaScript value or object equivalent.
parse() JSON parsing is the process of converting a JSON object in text format to a Javascript object that can be used inside a program. In Javascript, the standard way to do this is by using the method JSON. parse() , as the Javascript standard specifies.
Google "JSON Framework". Follow the (easy) instructions to install it.
Then go:
//let's say there's NSString *jsonString.
NSArray *userData = [jsonString JSONValue];
NSMutableArray *creditCards = [NSMutableArray array];
for (NSDictionary *user in userData) {
[creditCards addObject:[user objectForKey:@"creditCardNumber"]];
}
You'll drop out the bottom of that with NSMutableArray *creditCards full of NSString objects containing the credit card numbers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With