2013-04-27 26 views
1

我是Objective C編程的新手。我對Objective C & json響應有問題。我正在嘗試從Google字典API中獲取單詞定義。我得到了Json的迴應。但它的問題是我得到回調respose。這不容易被json解析。 的API到我打電話是處理Objective C中的Json CallBack 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]; 

但是我得到空字典。我哪裏出錯了?請幫助我。

回答

1

SBJSON無法解析方法調用,應該以僅返回JSON數據而不是完整回調方法調用的方式更改API調用。

嘗試刪除網址的回調參數返回象下面只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"}]}]}]} 
+0

我試圖刪除回調參數,但它給我一個錯誤。它從來沒有處理我的請求沒有回調參數。有什麼其他方式可以做到這一點? – 2013-04-29 05:24:44

+0

當我嘗試避免回調參數,它給了我空響應。 – 2013-04-29 06:46:32

0

你的JSON格式是不妥當的。與檢查 - http://json.parser.online.fr/

那就試試這個:像這樣

 NSArray *arrPrimaries=(NSArray *)[dictData objectForKey:@"primaries"]; 
+0

Yess .... Samir。你是對的。我的jsonp響應無效。它包含一些不能被json解析器解析的字符串。但是要取代每一個都是非常困難的。我能做什麼。 – 2013-04-29 07:48:38

0

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.. 

,並得到陣列沒有針對此問題的一種臨時解決方案。嘗試從響應開始剝離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響應。

+0

Thanx Vova。但我已經這樣做了,現在我能夠以json格式獲得響應。但有時它仍然給我null NSDictionary,有時它可以正常工作。這是因爲它是一種特別的解決方案嗎?我無法避免回調參數。我該怎麼辦? – 2013-04-29 06:41:58

+0

當我嘗試避免回調參數,它給了我空響應。 – 2013-04-29 06:45:51

+0

@ user2063425,是的,可能是因爲響應可能會隨時變化,所以此變通辦法不起作用。 – 2013-04-29 07:25:28