简体   繁体   中英

iPhone:Server response parsing issues

The below is my server response. I would like to parse and use it further.

responseString:

{
    "status": "1",
    "MyData": [
        {
            "Value": 1,
            "Name": "David",
            "MusicURL": "http://www.mycomany.net/musicoutput/song1.wav",
            "ImageURL": "http://www.mycomany.net/imageout/david.png"
        },
        {
            "Value": 2,
            "Name": "Martin",
            "MusicURL": "http: //www.mycomany.net/musicoutput/song1.wav",
            "ImageURL": "http: //www.mycomany.net/imageout/martin.png"
        },
        {
            "Value": 3,
            "Name": "Steve",
            "MusicURL": "http: //www.mycomany.net/musicoutput/song1.wav",
            "ImageURL": "http: //www.mycomany.net/imageout/david.png"
        }
    ]
}

The below is my code for trying further to parse and use it. BUT, the issue is, its not getting parsed from NSJSONSerialization, instead i'm getting NULL. Printing as responseDict:(null) ; jsonError: (null).

Please Note ,when i'm doing like this->NSString *responseString = [[NSString alloc] initWithData:theResponseData encoding:NSUTF8StringEncoding]; , i'm getting response string as the above output. But, i want it as NSDictionary for my further use.

Please help to resolve this issue.

NSMutableData *webData;

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [webData setLength: 0];
    //    [self callGetGroup];    
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{    
    [webData appendData:data];
}

        -(void)connectionDidFinishLoading:(NSURLConnection *)connection
        {

        NSData *theResponseData = [[NSData alloc] initWithData:webData];
            if (theResponseData)
            {
    //NSString *responseString = [[NSString alloc] initWithData:theResponseData encoding:NSUTF8StringEncoding];
     //NSLog(@"responseString:%@ ;", responseString);
                NSError *jsonError;
                NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:theResponseData options:0 error:&jsonError];

        NSLog(@"responseDict:%@ ; jsonError: %@", responseDict, jsonError); // Printing as responseDict:(null) ; jsonError: (null)

        [self handleResponse :responseDict];

            }
        }


        -(void) handleResponse :(NSDictionary *) responsedata
        {
            NSString* value = NULL;

            for (id key in responsedata)
            {
                NSDictionary *currentDict = (NSDictionary *) [responsedata objectForKey:key];
                value = (NSString*)[currentDict objectForKey:@"status"];

                if ( [value intValue]==1) // success
                {

                }

            }

        }

You did not tell us what the error was.

Anyway, there is a mistake in the JSON Feed, so there is most likely a parsing error. If the JSON is not correct, the resulting object will be nil . Output from JSONLint :

Parse error on line 13:
...        "MusicURL": http: //mycpmany.net
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['

Try this, the "key"is not the object for the value in resposeDictionary, the ResposeDictionary holds more dictionaries from the look of the data. So your "key" is not the key to a value, it's a dictionary. It's a little hard to read the output the way it's written in the question but:

for (id object in responsedata)
{
    NSDictionary *currentDict = (NSDictionary *)object;
    value = (NSString*)[currentDict valueForKey:@"status"];
    if ( [value length] > 0) // success
    {
        NSLog(@"Success! Status:%@",value);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM