我使用的觸摸JSON對我來說工作得很好。我能夠獲得一個數組,把它放在一個字典中,通過touchJSON序列化並通過http發送出去。如何從nsdictionary對象中獲取名稱和url的值?
現在在返回的一端,我收到了數據,並將其放入字典中(我使用twitter中的trends.json作爲示例JSON)。
如果我試圖讓從字典對象趨勢值,我得到這個:
2010-08-02 00:23:31.069 rateMyTaxi[30610:207] ANSWER: (
{
name = "Fried Chicken Flu";
url = "http://search.twitter.com/search?q=Fried+Chicken+Flu";
},
{
name = "Lisa Simpson";
url = "http://search.twitter.com/search?q=Lisa+Simpson";
},
{
name = "#breakuplines";
url = "http://search.twitter.com/search?q=%23breakuplines";
},
{
name = "#thingsuglypeopledo";
url = "http://search.twitter.com/search?q=%23thingsuglypeopledo";
},
{
name = "Inception";
url = "http://search.twitter.com/search?q=Inception";
},
{
name = "#sharkweek";
url = "http://search.twitter.com/search?q=%23sharkweek";
},
{
name = "JailbreakMe";
url = "http://search.twitter.com/search?q=JailbreakMe";
},
{
name = "Kourtney";
url = "http://search.twitter.com/search?q=Kourtney";
},
{
name = "Shark";
url = "http://search.twitter.com/search?q=Shark";
},
{
name = "Boondocks";
url = "http://search.twitter.com/search?q=Boondocks";
}
)
如果我試圖讓該值的名稱或網址,我什麼也得不到這是令人沮喪的。這是我需要的數據。你可以告訴它是字典格式,因爲它的格式是正確的,它正在閱讀適當的趨勢。我很確定我錯過了一些東西,所以請讓我知道要遵循的方向。
下面是代碼:
// this is all touch JSON magic. responseString has the full contents of trends.json
NSString *response = [request responseString];
NSLog(@"response value is:%@",response);
NSString *jsonString = response;
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF32BigEndianStringEncoding];
NSError *error = nil;
NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
//end of touchJSON. It is in a dictionary now.
NSLog(@"dictionary:%@, error %@", dictionary, error); //http://cl.ly/adb6c6a974c3e70fb51c
NSString *twitterTrends = (NSString *) [dictionary objectForKey:@"trends"];
NSLog(@"ANSWER:%@",twitterTrends); //http://cl.ly/fe270fe7f05a0ea8d478
謝謝,這是我正在尋找。我拿了一個級別,並提取該代碼中的趨勢詞典。這裏是最後的代碼 for(NSDictionary * item in [dictionary objectForKey:@「trends」]){NEXT string * name = [item objectForKey:@「name」]; NSString * url = [item objectForKey:@「url」]; – 2010-08-02 14:15:52