0

代码:

+(id)loadJSONDataFromURL:(NSString *)urlString{

 MsgsHelper *msg=[[MsgsHelper alloc]init];
// This function takes the URL of a web service, calls it, and either returns "nil", or a NSDictionary,
// describing the JSON data that was returned.

NSError *error;
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

// Call the web service, and (if it's successful) store the raw data that it returns
 NSURLResponse *response = nil; //
NSData *data = [ NSURLConnection sendSynchronousRequest:request returningResponse: &response error:&error ];



//here we get the respond from NSURLResponse and then we check for the statusCode //1
//200 is ok,, 0 is no internet connection else is server error //2

NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;//1
//any warning like this one with integers shuold add the casting (int)
int statCode = (int)[httpResponse statusCode];//2
if(statCode == 200){

if (!data)
{
    //NSLog(@"Download Error: %@", error.localizedDescription);
    return nil;
}

// Parse the (binary) JSON data from the web service into an NSDictionary object
id dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if (dictionary == nil) {
    //NSLog(@"JSON Error: %@", error);
    return nil;
}

return dictionary;

}else if(statCode == 0){


    [msg alertStatus:NSLocalizedString(@"No internet", @"Message") :@"" :0   ];
    return nil;
}else{
    //Server Error
    //NSLog(@"Server Error");
    [msg alertStatus:NSLocalizedString(@"Server Error", @"Message") :@"" :0   ];

    return nil;
}}

结果问题如下:

results: (
        {
        "Name" = "\U0633\U0639\U0648\U062f \U0639\U0628\U062f\U0627\U0644\U0639\U0632\U064a\U0632 \U064a\U0646 \U062c\U062f\U064a\U062f";
        "location" = CENTER;
       }
)

也许需要 dataUsingEncoding 来读取阿拉伯字符串,但是怎么做。

谢谢,

4

0 回答 0