1

我是 Objective C 编程的新手。我对 Objective C & json 响应有疑问。我正在尝试从 Google 词典 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 无法解析方法调用,您应该更改 API 调用以仅返回 JSON 数据而不是完整的回调方法调用。

尝试删除 url 的回调参数以仅返回如下所示的 JSON 数据,这是有效且可解析的:

 {"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 回答
0

这个问题有一个特别的解决方案。尝试dict_api.callbacks.id100(从响应的开头和,200,null)结尾剥离。就像是:

responseString = [responseString componentsSeparatedByString:@"dict_api.callbacks.id100("][1];
responseString = [responseString componentsSeparatedByString:@",200,null)"][0];

SBJSON *objJSONParser = [[SBJSON alloc] init];
NSDictionary *dictData = [objJSONParser objectWithString:strResponse error:nil];
// ... so on

请注意,这是一个临时解决方案,它可能不适用于任何其他响应。遵循@ararog 的建议:查找 API 以获取仅限 JSON 的响应。

于 2013-04-27T12:31:42.817 回答