簡體   English   中英

iPhone:服務器響應解析問題

[英]iPhone:Server response parsing issues

以下是我的服務器響應。 我想解析並進一步使用它。

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"
        }
    ]
}

以下是我的代碼,用於嘗試進一步解析和使用它。 但是,問題是,它沒有從NSJSONSerialization中解析出來,相反,我得到了NULL。 打印為responseDict:(null); jsonError :(空)。

請注意 ,當我這樣做時-> NSString * responseString = [[NSString alloc] initWithData:theResponseData編碼:NSUTF8StringEncoding]; ,我正在將響應字符串作為上述輸出。 但是,我希望它可以作為NSDictionary供我進一步使用。

請幫助解決此問題。

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
                {

                }

            }

        }

您沒有告訴我們錯誤是什么。

無論如何,JSON Feed中有一個錯誤,因此很可能是解析錯誤。 如果JSON不正確,則生成的對象將為nil JSONLint的輸出:

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

嘗試此操作,“鍵”不是resposeDictionary中的值的對象,從數據的外觀來看,ResposeDictionary可容納更多的字典。 因此,您的“鍵”不是值的鍵,而是字典。 用問題中的輸出很難讀取輸出,但是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM