Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to parse array of objects using json for iphone

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.

like image 761
rithik Avatar asked Feb 11 '11 18:02

rithik


People also ask

What is JSON parsing in iOS?

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.

Can you JSON parse an array?

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.

Can you parse a JSON object?

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.


1 Answers

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.

like image 127
Dan Ray Avatar answered Oct 20 '22 00:10

Dan Ray