gpt4 book ai didi

ios - 将数据传递到 Web 服务时出错。 'NSInvalidArgumentException' , 原因 : 'Invalid (non-string) key in JSON dictionary'

转载 作者:行者123 更新时间:2023-11-28 22:00:09 24 4
gpt4 key购买 nike

我正在尝试将我保存在 UserDefaults 上的 Twitter 提要传递给 WebService。 WebService 有效,因为我在传递来自 Facebook 的提要时使用它。

- (void) fetchedTwitterFeeds: (NSString *)screenname{
STTwitterAPI * twit = [STTwitterAPI twitterAPIAppOnlyWithConsumerKey:twitter_id consumerSecret:twitter_secret];

if ([[NSUserDefaults standardUserDefaults] valueForKey:TWITTER_SCREEN_NAME] !=nil) {
screenname = [NSString stringWithFormat:@"%@", [[NSUserDefaults standardUserDefaults] valueForKey:TWITTER_SCREEN_NAME]];
}

[twit verifyCredentialsWithSuccessBlock:^(NSString * username){
[twit getUserTimelineWithScreenName:screenname successBlock:^(NSArray * statuses){
self.feeds = [NSMutableArray arrayWithArray:statuses];
NSLog(@"Twitter Feeds Login: %@", feeds);

NSLog(@"Session Token: %@", [[NSUserDefaults standardUserDefaults] valueForKey:SESSION_TOKEN]);
NSLog(@"Track ID: %d", track_id);
NSLog(@"Tweets: %@", [feeds valueForKey:@"text"]);
NSLog(@"Created at: %@", [feeds valueForKey:@"created_at"]);
NSLog(@"Country: %@", [[feeds valueForKey:@"place"] valueForKey:@"country"]);
NSLog(@"Place Name: %@", [[feeds valueForKey:@"place"] valueForKey:@"full_name"]);
NSLog(@"Sample Lat: -75.14310264");
NSLog(@"Sample Long: 40.05701649");
NSLog(@"Media URL: %@", [[[feeds valueForKey:@"entities"] valueForKey:@"media"] valueForKey:@"media_url"]);
NSLog(@"Provider Name: %@", TWITTER_PROVIDER);

//Saving Feeds
if ([feeds valueForKey:@"text"] !=nil)[[NSUserDefaults standardUserDefaults] setObject:[feeds valueForKey:@"text"] forKey:TWITTER_FEED_TWEETS_KEY];
else[[NSUserDefaults standardUserDefaults] setObject:@"No Tweets" forKey:TWITTER_FEED_TWEETS_KEY];

if ([feeds valueForKey:@"created_at"] !=nil)[[NSUserDefaults standardUserDefaults] setObject:[feeds valueForKey:@"created_at"] forKey:TWITTER_FEED_DATE_CREATED_KEY];
else[[NSUserDefaults standardUserDefaults] setObject:@"No Date Created" forKey:TWITTER_FEED_DATE_CREATED_KEY];

if ([feeds valueForKey:@"created_at"] !=nil)[[NSUserDefaults standardUserDefaults] setObject:[[feeds valueForKey:@"place"] valueForKey:@"country"] forKey:TWITTER_FEED_COUNTRY_KEY];
else[[NSUserDefaults standardUserDefaults] setObject:@"No Country" forKey:TWITTER_FEED_COUNTRY_KEY];

if ([feeds valueForKey:@"created_at"] !=nil)[[NSUserDefaults standardUserDefaults] setObject:[[feeds valueForKey:@"place"] valueForKey:@"full_name"] forKey:TWITTER_FEED_PLACE_NAME_KEY];
else[[NSUserDefaults standardUserDefaults] setObject:@"No Place Name" forKey:TWITTER_FEED_PLACE_NAME_KEY];

if ([feeds valueForKey:@"created_at"] !=nil)[[NSUserDefaults standardUserDefaults] setObject:@"-75.14310264" forKey:TWITTER_FEED_LATITUDE_KEY];

if ([feeds valueForKey:@"created_at"] !=nil)[[NSUserDefaults standardUserDefaults] setObject:@"40.05701649" forKey:TWITTER_FEED_LONGITUDE_KEY];

if ([feeds valueForKey:@"created_at"] !=nil)[[NSUserDefaults standardUserDefaults] setObject:[[[feeds valueForKey:@"entities"] valueForKey:@"media"] valueForKey:@"media_url"] forKey:TWITTER_FEED_MEDIA_URL_KEY];
else[[NSUserDefaults standardUserDefaults] setObject:@"No Media " forKey:TWITTER_FEED_MEDIA_URL_KEY];

}errorBlock:^(NSError * error){
NSLog(@"%@", error.debugDescription);
}];
}errorBlock:^(NSError * error){
NSLog(@"%@", error.debugDescription);
}];
}

这是我在 NSLog(@"Twitter Feeds Login: %@", feeds) 时得到的 Twitter Feeds:

    2014-08-19 15:42:10.497 backtrack[11009:60b] Twitter Feeds Login: (
{
contributors = "<null>";
coordinates = "<null>";
"created_at" = "Tue Aug 19 04:50:45 +0000 2014";
entities = {
hashtags = (
);
media = (
{
"display_url" = "pic.twitter.com/kKBrsjf4GO";
"expanded_url" = "http://twitter.com/calsofer/status/501592079156273152/photo/1";
id = 501592077608562689;
"id_str" = 501592077608562689;
indices = (
5,
27
);
"media_url" = "http://pbs.twimg.com/media/BvYDVkgCMAEBucu.png";
"media_url_https" = "https://pbs.twimg.com/media/BvYDVkgCMAEBucu.png";
sizes = {
large = {
h = 120;
resize = fit;
w = 120;
};
medium = {
h = 120;
resize = fit;
w = 120;
};
small = {
h = 120;
resize = fit;
w = 120;
};
thumb = {
h = 120;
resize = crop;
w = 120;
};
};
type = photo;
url = "http://t.co/kKBrsjf4GO";
}
);
symbols = (
);
urls = (
);
"user_mentions" = (
);
};
"extended_entities" = {
media = (
{
"display_url" = "pic.twitter.com/kKBrsjf4GO";
"expanded_url" = "http://twitter.com/calsofer/status/501592079156273152/photo/1";
id = 501592077608562689;
"id_str" = 501592077608562689;
indices = (
5,
27
);
"media_url" = "http://pbs.twimg.com/media/BvYDVkgCMAEBucu.png";
"media_url_https" = "https://pbs.twimg.com/media/BvYDVkgCMAEBucu.png";
sizes = {
large = {
h = 120;
resize = fit;
w = 120;
};
medium = {
h = 120;
resize = fit;
w = 120;
};
small = {
h = 120;
resize = fit;
w = 120;
};
thumb = {
h = 120;
resize = crop;
w = 120;
};
};
type = photo;
url = "http://t.co/kKBrsjf4GO";
}
);
};
"favorite_count" = 0;
favorited = 0;
geo = "<null>";
id = 501592079156273152;
"id_str" = 501592079156273152;
"in_reply_to_screen_name" = "<null>";
"in_reply_to_status_id" = "<null>";
"in_reply_to_status_id_str" = "<null>";
"in_reply_to_user_id" = "<null>";
"in_reply_to_user_id_str" = "<null>";
lang = en;
place = {
attributes = {
};
"bounding_box" = {
coordinates = (
(
(
"-8.662663",
"49.1626564"
),
(
"1.768926",
"49.1626564"
),
(
"1.768926",
"60.86165"
),
(
"-8.662663",
"60.86165"
)
)
);
type = Polygon;
};
"contained_within" = (
);
country = "United Kingdom";
"country_code" = GB;
"full_name" = "United Kingdom";
id = 6416b8512febefc9;
name = "United Kingdom";
"place_type" = country;
url = "https://api.twitter.com/1.1/geo/id/6416b8512febefc9.json";
};
"possibly_sensitive" = 0;
"retweet_count" = 0;
retweeted = 0;
source = "<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>";
text = "Test http://t.co/kKBrsjf4GO";
truncated = 0;
user = {
"contributors_enabled" = 0;
"created_at" = "Sat Jan 29 11:30:22 +0000 2011";
"default_profile" = 0;
"default_profile_image" = 0;
description = "You can't just grow a sexy beard. You either have one, or you don't..:";
entities = {
description = {
urls = (
);
};
};
"favourites_count" = 0;
"follow_request_sent" = "<null>";
"followers_count" = 17;
following = "<null>";
"friends_count" = 47;
"geo_enabled" = 1;
id = 244472426;
"id_str" = 244472426;
"is_translation_enabled" = 0;
"is_translator" = 0;
lang = en;
"listed_count" = 0;
location = "";
name = "Christopher Fuertes";
notifications = "<null>";
"profile_background_color" = 1A1B1F;
"profile_background_image_url" = "http://pbs.twimg.com/profile_background_images/558638885/559647_3164538518462_1415356240_32122022_1669491008_n.jpg";
"profile_background_image_url_https" = "https://pbs.twimg.com/profile_background_images/558638885/559647_3164538518462_1415356240_32122022_1669491008_n.jpg";
"profile_background_tile" = 1;
"profile_banner_url" = "https://pbs.twimg.com/profile_banners/244472426/1352679509";
"profile_image_url" = "http://pbs.twimg.com/profile_images/2838158026/56ed90e6ccc6c35cc41a1a8a41db312e_normal.jpeg";
"profile_image_url_https" = "https://pbs.twimg.com/profile_images/2838158026/56ed90e6ccc6c35cc41a1a8a41db312e_normal.jpeg";
"profile_link_color" = 960E20;
"profile_sidebar_border_color" = FFFFFF;
"profile_sidebar_fill_color" = 252429;
"profile_text_color" = 666666;
"profile_use_background_image" = 1;
protected = 0;
"screen_name" = calsofer;
"statuses_count" = 1;
"time_zone" = "Kuala Lumpur";
url = "<null>";
"utc_offset" = 28800;
verified = 0;
};
}
)

这是我将其传递给网络服务的代码:

 NSLog(@"UserDefault Tweets: %@", [[NSUserDefaults standardUserDefaults] objectForKey:TWITTER_FEED_TWEETS_KEY]);
NSLog(@"UserDefault Date Created: %@", [[NSUserDefaults standardUserDefaults] objectForKey:TWITTER_FEED_DATE_CREATED_KEY]);
NSLog(@"UserDefault Country: %@", [[NSUserDefaults standardUserDefaults] objectForKey:TWITTER_FEED_COUNTRY_KEY]);
NSLog(@"UserDefault Place Name: %@", [[NSUserDefaults standardUserDefaults] objectForKey:TWITTER_FEED_PLACE_NAME_KEY]);
NSLog(@"UserDefault Latitude: %@", [[NSUserDefaults standardUserDefaults] objectForKey:TWITTER_FEED_LATITUDE_KEY]);
NSLog(@"UserDefault Longtitude: %@", [[NSUserDefaults standardUserDefaults] objectForKey:TWITTER_FEED_LONGITUDE_KEY]);
NSLog(@"UserDefault Media URL: %@", [[NSUserDefaults standardUserDefaults] objectForKey:TWITTER_FEED_MEDIA_URL_KEY]);

NSDictionary * dict = [NSDictionary dictionaryWithObject:[NSArray arrayWithObjects: [[NSUserDefaults standardUserDefaults] valueForKey:SESSION_TOKEN], [NSString stringWithFormat:@"%d", track_id], [[NSUserDefaults standardUserDefaults] valueForKey:TWITTER_FEED_TWEETS_KEY], [[NSUserDefaults standardUserDefaults] valueForKey:TWITTER_FEED_DATE_CREATED_KEY], [[NSUserDefaults standardUserDefaults] valueForKey:TWITTER_FEED_COUNTRY_KEY], [[NSUserDefaults standardUserDefaults] valueForKey:TWITTER_FEED_PLACE_NAME_KEY], [[NSUserDefaults standardUserDefaults] valueForKey:TWITTER_FEED_LATITUDE_KEY], [[NSUserDefaults standardUserDefaults] valueForKey:TWITTER_FEED_LONGITUDE_KEY], [[NSUserDefaults standardUserDefaults] valueForKey:TWITTER_FEED_MEDIA_URL_KEY],TWITTER_PROVIDER, nil]
forKey:[NSArray arrayWithObjects:SESSION_TOKEN, @"track_id", @"message",@"date_created",@"country",@"place_name",@"latitude",@"longitude",@"media_url",@"provider_name", nil]];

[webService postRequestFromUrl:[NSString stringWithFormat:@"%@%@", ROOT_SERVER_URL, SAVE_DATA_FEEDS] withDictionary:dict];

但我在这样做时遇到了错误。错误是:

enter image description here

最佳答案

当你创建 dict 时,你应该使用 dictionaryWithObjects:forKeys:,而不是 dictionaryWithObject:forKey: - 你正在指定 NSArray 作为您的键,异常告诉您,它不是字符串。

你应该有 -

NSDictionary * dict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects: [[NSUserDefaults standardUserDefaults] valueForKey:SESSION_TOKEN], [NSString stringWithFormat:@"%d", track_id], [[NSUserDefaults standardUserDefaults] valueForKey:TWITTER_FEED_TWEETS_KEY], [[NSUserDefaults standardUserDefaults] valueForKey:TWITTER_FEED_DATE_CREATED_KEY], [[NSUserDefaults standardUserDefaults] valueForKey:TWITTER_FEED_COUNTRY_KEY], [[NSUserDefaults standardUserDefaults] valueForKey:TWITTER_FEED_PLACE_NAME_KEY], [[NSUserDefaults standardUserDefaults] valueForKey:TWITTER_FEED_LATITUDE_KEY], [[NSUserDefaults standardUserDefaults] valueForKey:TWITTER_FEED_LONGITUDE_KEY], [[NSUserDefaults standardUserDefaults] valueForKey:TWITTER_FEED_MEDIA_URL_KEY],TWITTER_PROVIDER, nil]
forKeys:[NSArray arrayWithObjects:SESSION_TOKEN, @"track_id", @"message",@"date_created",@"country",@"place_name",@"latitude",@"longitude",@"media_url",@"provider_name", nil]];

如果您只是将变量设置为 [NSUserDefaults standardUserDefaults] 并引用它而不是重复调用 standardUserDefaults 方法,您的代码也会更容易阅读

关于ios - 将数据传递到 Web 服务时出错。 'NSInvalidArgumentException' , 原因 : 'Invalid (non-string) key in JSON dictionary' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25380806/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com