2016-05-05 5 views
-1

Ich bin neu in iOS-Entwicklung. Ich versuche, JSOn-Array-Werte zu Objective-C-Werten zu konvertieren. Meine JSON-Werte sind wie folgt aus:Konvertieren JSON-Array zu Objective-C in iOS

{"result": 
     [{"alternative": 
        [{"transcript":"4"}, 
        {"transcript":"four"}, 
        {"transcript":"so"}, 
        {"transcript":"who"}], 
    "final":true}], 
"result_index":0} 

ich es auf diese Weise versucht haben:

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:speechrequestString]]; 

NSError *error; 
NSDictionary *speechResult= [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

NSArray *speechArray= [speechResult valueForKey:@"result"]; 
NSLog(@"%@",speechArray); 

NSLog(@"Response is of type: %@", [speechArray class]); 

speechArray immer null ist. Wie löst man dieses Problem?
Gleichzeitig möchte ich transcript Werte drucken.

+0

W ist das der Fehler? – Avi

+0

redeArray-Wert wird immer null ausgegeben. Danke @ Avi – amsaraj

Antwort

0

Ich denke, das Problem könnte in der Initialisierung des Arrays und des Wörterbuchs sein.

Versuchen Sie dies tun,

NSDictionary *speechResult = [NSDictionary new]; 

NSArray *speechArray = [NSArray new]; 

Hope it ..

0

hilft Sie haben das Array zu initialisieren, bevor es verwendet wird,

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:speechrequestString]]; 

NSError *error; 
NSDictionary *speechResult= [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

NSArray *speechArray= [[NSArray alloc] init]; 
speechArray= [speechResult valueForKey:@"result"]; 
if (speechArray.count>0) { 
    NSDictionary * alternativeDictn = [speechArray objectAtIndex:0]; 
    NSArray *alternativeAry= [[NSArray alloc] init]; 
    alternativeAry = [alternativeDictn objectForKey:@"alternative"]; 
    NSLog(@"%@",alternativeAry); 
} 

NSLog(@"Response is of type: %@", [speechArray class]); 

mit diesem Code werden Sie Ergebnis erhalten folgende,

(
     { 
     transcript = 4; 
    }, 
     { 
     transcript = four; 
    }, 
     { 
     transcript = so; 
    }, 
     { 
     transcript = who; 
    } 
) 
+0

Ich möchte Transkriptwerte drucken. [{"Transkript": "4"}, {"Transkript": "vier"}, {"Transkript": "so"}, {"Transkript": "wer"}], – amsaraj

+0

@amsaraj Überprüfen Sie meine aktualisierte Antwort, ich habe es entsprechend Ihrer Notwendigkeit geändert – Dilip

+0

@amsaraj lassen Sie mich wissen, wenn Sie irgendein Problem gegenüberstellen. – Dilip

0

versuchen dies auch sein mag es wird Ihnen helfen: -

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:speechrequestString]]; 

NSError *error; 
NSDictionary *speechResult = [NSDictionary new]; 
speechResult= [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

NSArray *speechArray = [[NSArray alloc]init]; 
speechArray= [speechResult valueForKey:@"result"]; 
NSLog(@"%@",speechArray); 

NSLog(@"Response is of type: %@", [speechArray class]); 
0
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];//data is your response from server as NSData 

if ([json isKindOfClass:[NSDictionary class]]){ //Added instrospection as suggested in comment. 
    NSArray * speechArray = json[@"result"]; 
} 

NSLog(@"speech array %@",speechArray); 
0

Haben Sie den Wert in NSDictionary (speechResult) überprüfen.

Wenn es Null ist, überprüfen Sie die JSON-Daten isValid oder nicht.

NSError *error; 
    if ([NSJSONSerialization JSONObjectWithData:data 
             options:kNilOptions 
              error:&error] == nil) 
    { 
     // Check the error here 
    }else { 
     // here check whether it is a dictionary and it has key like @Bhadresh Mulsaniya mentioned. 
    } 

Wenn NULL zurückgegeben wird, überprüfen Sie den Fehler, um zu verstehen, was falsch ist.

0

diesen Code versuchen,

restaurantdictionary = [NSJSONSerialization JSONObjectWithData:mutableData options:NSJSONReadingMutableContainers error:&e]; 

NSMutableArray *main_array=[[NSMutableArray alloc]init]; 

main_array=[restaurantdictionary valueForKey:@"results"]; 

NSLog(@"main_array %@", main_array); 

seine für mich arbeiten, hoffen, dass seine hilfreich

0

Versuchen Sie, den folgenden Code aus der Transkripte zu erhalten:

NSData* jsonData = [yourJsonString dataUsingEncoding:NSUTF8StringEncoding]; 

    NSError *error = nil; 
    NSDictionary *responseObj = [NSJSONSerialization 
           JSONObjectWithData:jsonData 
           options:0 
           error:&error]; 

    if(! error) { 
     NSArray *responseArray = [responseObj objectForKey:@"result"]; 
     for (NSDictionary *alternative in responseArray) { 
      NSArray *altArray = [alternative objectForKey:@"alternative"]; 
      for (NSDictionary *transcript in altArray) { 
       NSLog(@"transcript : %@",[transcript objectForKey:@"transcript"]); 
      } 
     } 

    } else { 
     NSLog(@"Error in parsing JSON"); 
    } 
0

Sie Ihr Wörterbuch ersten Alloc sollte wie diese-

NSDictionary *speechResult = [[NSDictionary alloc]init]; 
    speechResult= [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 
    NSArray *speechArray = [[NSArray alloc]init]; 
    speechArray= [speechResult valueForKey:@"result"];