1

Objective C プログラミングは初めてです。Objective C と json の応答に問題があります。Google Dictionary API から単語の定義を取得しようとしています。Json レスポンスを取得しています。しかし、それに関する問題は、コールバックの応答を取得していることです。これはjsonで簡単に解析できません。私が呼び出しているAPIは

http://www.google.com/dictionary/json?callback=dict_api.callbacks.id100&q=test&sl=en&tl=en&restrict=prCde&client=te

そして、私は次のように応答を得ています

dict_api.callbacks.id100({"query":"test","sourceLanguage":"en","targetLanguage":"en","primaries":[{"type":"headword","terms":[{"type":"text","text":"test","language":"en","labels":[{"text":"Noun","title":"Part-of-speech"}]},{"type":"phonetic","text":"/test/","language":"und"},{"type":"sound","text":"http://www.gstatic.com/dictionary/static/sounds/de/0/test.mp3","language":"und"}],"entries":[{"type":"related","terms":[{"type":"text","text":"tests","language":"und","labels":[{"text":"plural"}]}]},{"type":"meaning","terms":[{"type":"text","text":"A procedure intended to establish the quality, performance, or reliability of something, esp. before it is taken into widespread use","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"no sparking was visible during the \x3cem\x3etests\x3c/em\x3e","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"A short written or spoken examination of a person\x27s proficiency or knowledge","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"a spelling \x3cem\x3etest\x3c/em\x3e","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"An event or situation that reveals the strength or quality of someone or something by putting them under strain","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"this is the first serious \x3cem\x3etest\x3c/em\x3e of the peace agreement","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"An examination of part of the body or a body fluid for medical purposes, esp. by means of a chemical or mechanical procedure rather than simple inspection","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"a \x3cem\x3etest\x3c/em\x3e for HIV","language":"en"}]},{"type":"example","terms":[{"type":"text","text":"eye \x3cem\x3etests\x3c/em\x3e","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"A procedure employed to identify a substance or to reveal the presence or absence of a constituent within a substance","language":"en"}]},{"type":"meaning","terms":[{"type":"text","text":"The result of a medical examination or analytical procedure","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"a positive \x3cem\x3etest\x3c/em\x3e for protein","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"A means of establishing whether an action, item, or situation is an instance of a specified quality, esp. one held to be undesirable","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"a statutory \x3cem\x3etest\x3c/em\x3e of obscenity","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"A movable hearth in a reverberating furnace, used for separating gold or silver from lead","language":"en"}]}]},{"type":"headword","terms":[{"type":"text","text":"test","language":"en","labels":[{"text":"Verb","title":"Part-of-speech"}]},{"type":"phonetic","text":"/test/","language":"und"},{"type":"sound","text":"http://www.gstatic.com/dictionary/static/sounds/de/0/test.mp3","language":"und"}],"entries":[{"type":"related","terms":[{"type":"text","text":"tests","language":"und","labels":[{"text":"3rd person singular present"}]},{"type":"text","text":"tested","language":"und","labels":[{"text":"past participle"}]},{"type":"text","text":"testing","language":"und","labels":[{"text":"present participle"}]},{"type":"text","text":"tested","language":"und","labels":[{"text":"past tense"}]}]},{"type":"meaning","terms":[{"type":"text","text":"Take measures to check the quality, performance, or reliability of (something), esp. before putting it into widespread use or practice","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"this range has not been \x3cem\x3etested\x3c/em\x3e on animals","language":"en"}]},{"type":"example","terms":[{"type":"text","text":"the \x3cem\x3etesting\x3c/em\x3e and developing of prototypes","language":"en"}]},{"type":"example","terms":[{"type":"text","text":"a useful way to \x3cb\x3e\x3cem\x3etest\x3c/em\x3e out\x3c/b\x3e ideas before implementation","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"Reveal the strengths or capabilities of (someone or something) by putting them under strain","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"such behavior would severely \x3cem\x3etest\x3c/em\x3e any marriage","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"Give (someone) a short written or oral examination of their proficiency or knowledge","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"all children are \x3cem\x3etested\x3c/em\x3e at eleven","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"Judge or measure (someone\x27s proficiency or knowledge) by means of such an examination","language":"en"}]},{"type":"meaning","terms":[{"type":"text","text":"Carry out a medical \x3cem\x3etest\x3c/em\x3e on (a person, a part of the body, or a body fluid)","language":"en"}]},{"type":"meaning","terms":[{"type":"text","text":"Produce a specified result in a medical \x3cem\x3etest\x3c/em\x3e, esp. a drug \x3cem\x3etest\x3c/em\x3e or AIDS \x3cem\x3etest\x3c/em\x3e","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"he \x3cb\x3e\x3cem\x3etested\x3c/em\x3e positive\x3c/b\x3e for steroids during the race","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"Examine (a substance) by means of a reagent","language":"en"}]},{"type":"meaning","terms":[{"type":"text","text":"Touch or taste (something) to check that it is acceptable before proceeding further","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"she \x3cem\x3etested\x3c/em\x3e the water with the tip of her elbow","language":"en"}]}]}]},{"type":"headword","terms":[{"type":"text","text":"test","language":"en","labels":[{"text":"Noun","title":"Part-of-speech"}]},{"type":"sound","text":"http://www.gstatic.com/dictionary/static/sounds/de/0/test.mp3","language":"und"}],"entries":[{"type":"related","terms":[{"type":"text","text":"tests","language":"und","labels":[{"text":"plural"}]}]},{"type":"meaning","terms":[{"type":"text","text":"The shell or integument of some invertebrates and protozoans, esp. the chalky shell of a foraminiferan or the tough outer layer of a tunicate","language":"en"}]}]}]},200,null)

次のように、この応答を NSArray に保存しようとしています。

SBJSON *jsonParser = [[SBJSON alloc] init];
NSArray *responseArray = [jsonParser objectWithString:responseString];

私もNSdictionaryでこのコードを試しました。

SBJSON *jsonParser = [[SBJSON alloc] init];
NSDictionary *responseDict = [jsonParser objectWithString:responseString];

しかし、私はヌル辞書を取得しています。どこが間違っているのですか?私を助けてください。

4

3 に答える 3

1

SBJSON はメソッド呼び出しを解析できません。完全なコールバック メソッド呼び出しではなく、JSON データのみを返すように API 呼び出しを変更する必要があります。

以下のように、有効で解析可能な JSON データのみを返すように、url のコールバック パラメータを削除してみてください。

 {"query":"test","sourceLanguage":"en","targetLanguage":"en","primaries":[{"type":"headword","terms":[{"type":"text","text":"test","language":"en","labels":[{"text":"Noun","title":"Part-of-speech"}]},{"type":"phonetic","text":"/test/","language":"und"},{"type":"sound","text":"http://www.gstatic.com/dictionary/static/sounds/de/0/test.mp3","language":"und"}],"entries":[{"type":"related","terms":[{"type":"text","text":"tests","language":"und","labels":[{"text":"plural"}]}]},{"type":"meaning","terms":[{"type":"text","text":"A procedure intended to establish the quality, performance, or reliability of something, esp. before it is taken into widespread use","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"no sparking was visible during the \x3cem\x3etests\x3c/em\x3e","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"A short written or spoken examination of a person\x27s proficiency or knowledge","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"a spelling \x3cem\x3etest\x3c/em\x3e","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"An event or situation that reveals the strength or quality of someone or something by putting them under strain","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"this is the first serious \x3cem\x3etest\x3c/em\x3e of the peace agreement","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"An examination of part of the body or a body fluid for medical purposes, esp. by means of a chemical or mechanical procedure rather than simple inspection","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"a \x3cem\x3etest\x3c/em\x3e for HIV","language":"en"}]},{"type":"example","terms":[{"type":"text","text":"eye \x3cem\x3etests\x3c/em\x3e","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"A procedure employed to identify a substance or to reveal the presence or absence of a constituent within a substance","language":"en"}]},{"type":"meaning","terms":[{"type":"text","text":"The result of a medical examination or analytical procedure","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"a positive \x3cem\x3etest\x3c/em\x3e for protein","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"A means of establishing whether an action, item, or situation is an instance of a specified quality, esp. one held to be undesirable","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"a statutory \x3cem\x3etest\x3c/em\x3e of obscenity","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"A movable hearth in a reverberating furnace, used for separating gold or silver from lead","language":"en"}]}]},{"type":"headword","terms":[{"type":"text","text":"test","language":"en","labels":[{"text":"Verb","title":"Part-of-speech"}]},{"type":"phonetic","text":"/test/","language":"und"},{"type":"sound","text":"http://www.gstatic.com/dictionary/static/sounds/de/0/test.mp3","language":"und"}],"entries":[{"type":"related","terms":[{"type":"text","text":"tests","language":"und","labels":[{"text":"3rd person singular present"}]},{"type":"text","text":"tested","language":"und","labels":[{"text":"past participle"}]},{"type":"text","text":"testing","language":"und","labels":[{"text":"present participle"}]},{"type":"text","text":"tested","language":"und","labels":[{"text":"past tense"}]}]},{"type":"meaning","terms":[{"type":"text","text":"Take measures to check the quality, performance, or reliability of (something), esp. before putting it into widespread use or practice","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"this range has not been \x3cem\x3etested\x3c/em\x3e on animals","language":"en"}]},{"type":"example","terms":[{"type":"text","text":"the \x3cem\x3etesting\x3c/em\x3e and developing of prototypes","language":"en"}]},{"type":"example","terms":[{"type":"text","text":"a useful way to \x3cb\x3e\x3cem\x3etest\x3c/em\x3e out\x3c/b\x3e ideas before implementation","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"Reveal the strengths or capabilities of (someone or something) by putting them under strain","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"such behavior would severely \x3cem\x3etest\x3c/em\x3e any marriage","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"Give (someone) a short written or oral examination of their proficiency or knowledge","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"all children are \x3cem\x3etested\x3c/em\x3e at eleven","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"Judge or measure (someone\x27s proficiency or knowledge) by means of such an examination","language":"en"}]},{"type":"meaning","terms":[{"type":"text","text":"Carry out a medical \x3cem\x3etest\x3c/em\x3e on (a person, a part of the body, or a body fluid)","language":"en"}]},{"type":"meaning","terms":[{"type":"text","text":"Produce a specified result in a medical \x3cem\x3etest\x3c/em\x3e, esp. a drug \x3cem\x3etest\x3c/em\x3e or AIDS \x3cem\x3etest\x3c/em\x3e","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"he \x3cb\x3e\x3cem\x3etested\x3c/em\x3e positive\x3c/b\x3e for steroids during the race","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"Examine (a substance) by means of a reagent","language":"en"}]},{"type":"meaning","terms":[{"type":"text","text":"Touch or taste (something) to check that it is acceptable before proceeding further","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"she \x3cem\x3etested\x3c/em\x3e the water with the tip of her elbow","language":"en"}]}]}]},{"type":"headword","terms":[{"type":"text","text":"test","language":"en","labels":[{"text":"Noun","title":"Part-of-speech"}]},{"type":"sound","text":"http://www.gstatic.com/dictionary/static/sounds/de/0/test.mp3","language":"und"}],"entries":[{"type":"related","terms":[{"type":"text","text":"tests","language":"und","labels":[{"text":"plural"}]}]},{"type":"meaning","terms":[{"type":"text","text":"The shell or integument of some invertebrates and protozoans, esp. the chalky shell of a foraminiferan or the tough outer layer of a tunicate","language":"en"}]}]}]}
于 2013-04-27T11:13:53.227 に答える
0

JSON 形式が適切ではありません。で確認してください - http://json.parser.online.fr/

次にこれを試してください:

    NSString *strResponse = [request responseString];
    SBJSON *objJSONParser = [[SBJSON alloc] init];
    NSDictionary *dictData = [objJSONParser objectWithString:strResponse error:nil];

     NSDictionary *dictResult = [dictData objectForKey: @"response"];
     NSString *strQuery=[dictResult objectForKey:@"query"];.......... with your Key..

このような配列を取得します

     NSArray *arrPrimaries=(NSArray *)[dictData objectForKey:@"primaries"];
于 2013-04-27T11:21:04.127 に答える