Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

ios - How to extract JSON string to array?

Using http get request i did get this json string:

hello
[
    {
        "upid":"1",
        "uptitle":"test",
        "uptype":"test",
        "upsize":"1234",
        "upname":"test",
        "upuid":"1",
        "uplocation":"1",
        "uptimestamp":"2013-04-11 09:25:40"
    },
    {
        "upid":"17",
        "uptitle":"test",
        "uptype":"video/mov",
        "upsize":"2232",
        "upname":"testing",
        "upuid":"1",
        "uplocation":"",
        "uptimestamp":"2013-04-12 11:47:20"
    }
]

How i can get it output in format:

upid:1
uptitle:test
uptype:test
upsize:1234
upname:test
upuid:1
uplocation:1
uptimestamp:2013-04-11 09:25:40

upid:17
uptitle:test
uptype:video/mov
upsize:2232
upname:testing
upuid:1
uplocation:
uptimestamp:2013-04-12 11:47:20

Here is my code:

NSMutableURLRequest *GETrequest = [[NSMutableURLRequest alloc] init];
    [GETrequest setURL:[NSURL URLWithString:@"http:someip/upload/?id=1&command=alluseruploads&uid=1"]];
    [GETrequest setHTTPMethod:@"GET"];
    [GETrequest setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];

// And to send and receive an answer, use a NSURLResponse:
NSURLResponse *response;
NSData *GETReply = [NSURLConnection sendSynchronousRequest:GETrequest returningResponse:&response error:nil];
NSString *theReply = [[NSString alloc] initWithBytes:[GETReply bytes] length:[GETReply length] encoding: NSASCIIStringEncoding];
NSLog(@"Reply: %@", theReply);


// converting string to data
NSData *jsonData = [theReply dataUsingEncoding:NSUTF8StringEncoding];
//NSLog(@"dic = %@", jsonData);


NSError* error;

id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];

NSLog(@"%@", jsonObject);
See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

You don't have to import any frameworks as this uses a core iOS framework.

If you have an NSString called jsonString then you can do something like this...

NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

// you can skip this step by just using the NSData that you get from the http request instead of converting it to a string.

id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options: NSJSONReadingMutableContainers error:&localError];

NSLog(@"%@", jsonObject);

This will log out to the console your object. It will either be an NSArray or an NSDictionary and will contain instances of...

NSDate
NSDictionary
NSArray
NSNumber
NSString

You can then iterate this object to get the data.

OK, using your code...

NSURLResponse *response;
NSData *getData = [NSURLConnection sendSynchronousRequest:getRequest returningResponse:&response error:nil];

NSError* error;

id jsonObject = [NSJSONSerialization JSONObjectWithData:getData options: NSJSONReadingMutableContainers error:&error];

NSLog(@"%@", jsonObject);
NSLog(@"%@", error);

This should create your object. You don't need to convert to string and then back to data.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...