首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Objective C中处理Json CallBack API

在Objective C中处理Json CallBack API
EN

Stack Overflow用户
提问于 2013-04-27 18:58:41
回答 3查看 277关注 0票数 1

我是Objective C编程的新手。我有一个关于客观C& json响应的问题。我正在尝试从谷歌字典API获取单词定义。我得到了Json的回应。但它的问题是我得到了回调响应。它不能很容易地被json解析。我调用的API是

代码语言:javascript
运行
复制
http://www.google.com/dictionary/json?callback=dict_api.callbacks.id100&q=test&sl=en&tl=en&restrict=prCde&client=te

我得到的回应是

代码语言:javascript
运行
复制
/*
* 提示:该行代码过长,系统自动注释不进行高亮。一键复制会移除系统注释 
* 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中,如下所示:

代码语言:javascript
运行
复制
SBJSON *jsonParser = [[SBJSON alloc] init];
NSArray *responseArray = [jsonParser objectWithString:responseString];

就连我也用NSdictionary试过这段代码。

代码语言:javascript
运行
复制
SBJSON *jsonParser = [[SBJSON alloc] init];
NSDictionary *responseDict = [jsonParser objectWithString:responseString];

但是我得到的字典是空的。我哪里错了?请帮帮我。

EN

回答 3

Stack Overflow用户

发布于 2013-04-27 19:13:53

SBJSON不能解析方法调用,您应该以只返回JSON数据而不是完整的回调方法调用的方式来更改API调用。

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

代码语言:javascript
运行
复制
/*
* 提示:该行代码过长,系统自动注释不进行高亮。一键复制会移除系统注释 
* {"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"}]}]}]}
*/
票数 1
EN

Stack Overflow用户

发布于 2013-04-27 19:21:04

您的JSON格式不正确。使用- http://json.parser.online.fr/进行检查

然后试试这个:

代码语言:javascript
运行
复制
    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..

并像这样得到数组

代码语言:javascript
运行
复制
     NSArray *arrPrimaries=(NSArray *)[dictData objectForKey:@"primaries"];
票数 0
EN

Stack Overflow用户

发布于 2013-04-27 20:31:43

对于这个问题,有一个特别的解决方案。尝试从响应的开头去掉dict_api.callbacks.id100(,从结尾去掉,200,null)。类似于:

代码语言:javascript
运行
复制
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的响应。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16251293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档