1
如何將NSArray或NSDictionary的值傳遞給JSON.Its可能與否。現在我已經完成NSDictionary值和NSArray值轉換爲JSON.But我與PHP連接我的PHP代碼具有編碼和解碼方法與編碼和解碼如何可以從JSON獲得我的回覆嗎?以及如何從該響應中獲取值?將NSDictionary或NSArray POST成JSON?
我試圖轉換JSON格式以下代碼::
// Empty array
NSArray *emptyArray = @[];
// Single element array
NSArray *singleElementArray = @[@"Error Message"];
// Array of strings
NSArray *arrayOfStrings = @[@"First Name", @"Last Name"];
// Array of above arrays
NSArray *arrayOfObjects = @[emptyArray, singleElementArray, arrayOfStrings];
// Dictionary with several kay/value pairs and the above array of arrays
NSDictionary *dict = @{@"BlogName" : @"iOS Developer Tips",
@"BlogDomain" : @"iOSDeveloperTips.com",
@"Array" : arrayOfObjects};
NSError *error = nil;
NSData *json;
// Dictionary convertable to JSON ?
if ([NSJSONSerialization isValidJSONObject:dict])
{
// Serialize the dictionary
json = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
NSLog(@"json: %@", json);
// If no errors, let's view the JSON
if (json != nil && error == nil)
{
NSString *jsonString = [[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding];
NSLog(@"JSON conversion: %@", jsonString);
}
}
我從jsonString。我正確的價值觀有發佈此字符串JSON POST方法其工作正常,但我需要的NSDictionary的NSArray或後有到JSON.HOW?
你需要證明什麼是你的NSArray和你想要的JSON是什麼樣子。對於一個模糊的問題,答案太多了。 – 2015-02-09 13:21:53