我正在使用 touch JSON,这对我来说效果很好。我能够获取一个数组,将其放入字典中,通过 touchJSON 对其进行序列化,然后通过 http 将其发送出去。
现在在返回端,我收到了数据,并将其放入字典中(我使用来自 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";
}
)
如果我尝试获取名称或 URL 的值,我将得不到任何令人沮丧的结果。这就是我需要的数据。您可以判断它是字典格式,因为它已格式化并且可以正确读取趋势。我很确定我错过了一些东西,所以请让我知道要遵循的方向。
这是代码:
// 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