Dabei ist JSON_CATEGORY_DATA_URL_STRING
meine Feed-URL.
[
{
"group":"For Sale",
"code":"SSSS"
},
{
"group":"For Sale",
"category":"Wanted",
"code":"SWNT"
}
]
Ich kann anscheinend keine Nice NSDictionary
(oder NSArray
) aus dem folgenden Code erhalten:
+ (NSDictionary *)downloadJSON
{
NSDictionary *json_string;
NSString *dataURL = [NSString stringWithFormat:@"%@", JSON_CATEGORY_DATA_URL_STRING];
NSLog(@"%@",dataURL);
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:dataURL]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
json_string = [[[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]autorelease];
NSDictionary *json_dict = (NSDictionary *)json_string;
NSLog(@"json_dict\n%@",json_dict);
NSLog(@"json_string\n%@",json_string);
return json_string;
}
Ich habe viele Beiträge dazu gelesen, bekomme es aber nicht.
Mit IOS5 können Sie NSJSONSerialization zum Serialisieren des JSON verwenden.
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
Hoffe das hilft dir.
Sie können einen String nicht einfach als Wörterbuch umwandeln und erwarten, dass er den JSON-Code analysiert. Sie müssen eine JSON-Parsing-Bibliothek verwenden, um diese Zeichenfolge in ein Wörterbuch zu konvertieren.
Ich habe eine Klasse gemacht, die diese Aufgabe erleichtert. Es verwendet die NSJSONSerialization von iOS 5. Klon es von github hier .
Sie müssen den JSON-Parser verwenden. Hier ist der bearbeitete Code
+ (NSDictionary *)downloadJSON
{
NSDictionary *json_string;
NSString *dataURL = [NSString stringWithFormat:@"%@", JSON_CATEGORY_DATA_URL_STRING];
NSLog(@"%@",dataURL);
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:dataURL]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
json_string = [[[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]autorelease];
//JSONValue is a function that will return the appropriate object like dictionary or array depending on your json string.
NSDictionary *json_dict = [json_string JSONValue];
NSLog(@"json_dict\n%@",json_dict);
NSLog(@"json_string\n%@",json_string);
return json_dict;
}
dies sollte der Code sein, um das NSDictionary zu erhalten. aber Sie Json String ist ein Array, verwenden Sie stattdessen.
+ (NSArray *)downloadJSON
{
NSDictionary *json_string;
NSString *dataURL = [NSString stringWithFormat:@"%@", JSON_CATEGORY_DATA_URL_STRING];
NSLog(@"%@",dataURL);
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:dataURL]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
json_string = [[[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]autorelease];
NSArray *json_dict = [json_string JSONValue];
NSLog(@"json_dict\n%@",json_dict);
NSLog(@"json_string\n%@",json_string);
return json_dict;
}
Edit: Sie müssen JSON.framework verwenden, um die JSONValue-Methode aufzurufen.
Sie müssen auch json_dict
anstelle von json_string
zurückgeben, da json_string
vom Typ NSString
ist und nicht NSDictionary
oder NSArray
.
und nicht erneut freigeben, da dies Ihre Klassenvariable ist
erstellen Sie eine Methode, um Daten abzurufen. Geben Sie Ihre URL in URL mit String ein.
-(void)fetchjsondata
{
NSString *login= [[NSString stringWithFormat:@"your url string"]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"----%@", login);
NSURL *url = [NSURL URLWithString:[login stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//-- Get request and response though URL
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (data) {
dic_property= [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@", dic_property);
NSLog(@"counts=%d",[[dic_property objectForKey:@"Data"]count]);
}
else {
NSLog(@"network error, %@", [error localizedFailureReason]);
}
});
}];
}
rufen Sie fetchjsonmethod überall auf.
[NSThread detachNewThreadSelector: @selector (fetchdata) toTarget: self withObject: nil];