如何在 iOS 中正确地将 NSDictionary 转换为 json 格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11207483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to correctly convert an NSDictionary to a json format , in iOS?
提问by donparalias
I am trying to send to my server a json file. The json format should look exactly like this:
我正在尝试向我的服务器发送一个 json 文件。json 格式应如下所示:
{
"eventData": {
"eventDate": "Jun 13, 2012 12:00:00 AM",
"eventLocation": {
"latitude": 43.93838383,
"longitude": -3.46
},
"text": "hjhj",
"imageData": "raw data",
"imageFormat": "JPEG",
"expirationTime": 1339538400000
},
"type": "ELDIARIOMONTANES",
"title": "accIDENTE"
}
I hardcoded this and worked like this:
我对此进行了硬编码并像这样工作:
NSString *jsonString = @"{\"eventData\":{\"eventDate\":\"Jun 13, 2012 12:00:00 AM\",\"eventLocation\":{\"latitude\":43.93838383,\"longitude\":-3.46},\"text\":\"hkhkjh\",\"imageData\":\"\",\"imageFormat\":\"JPEG\",\"expirationTime\":1339538400000},\"type\":\"Culture\",\"title\":\"accIDENTE\"}";
So i want to do the exact same thing but using an NSDictionary cause of course i dont want to hardcode all my data. So i am creating an NSDictionary like this:
所以我想做完全相同的事情,但使用 NSDictionary 原因当然我不想硬编码我的所有数据。所以我正在创建一个这样的 NSDictionary:
NSMutableDictionary *eventLocation = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"43.93838383",@"latitude",@"-3.46", @"longitude" , nil];
NSMutableDictionary *eventData = [NSMutableDictionary dictionaryWithObjectsAndKeys:eventLocation,@"eventLocation", nil];
[eventData setObject:@"Jun 13, 2012 12:00:00 AM" forKey:@"eventDate"];
[eventData setObject:@"hjhj" forKey:@"text"];
[eventData setObject:@"raw data" forKey:@"imageData"];
[eventData setObject:@"JPEG" forKey:@"imageFormat"];
[eventData setObject:@"1339538400000" forKey:@"expirationTime"];
NSMutableDictionary *finalDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:eventData,@"eventData", nil];
[finalDictionary setObject:@"ELDIARIOMONTANES" forKey:@"type"];
[finalDictionary setObject:@"accIDENTE" forKey:@"title"];
Now i have to convert this to the correct json format , so that i can send it to my server. I tried using this :
现在我必须将其转换为正确的 json 格式,以便我可以将其发送到我的服务器。我尝试使用这个:
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:finalDictionary
options:NSJSONWritingPrettyPrinted
error:&error]
but now when i print the jsonData i get this :
但是现在当我打印 jsonData 时,我得到了这个:
jsonData: <7b0a2020 22657665 6e744461 74612220 3a207b0a 20202020 22696d61 6765466f 726d6174 22203a20 224a5045 47222c0a 20202020 22746578 7422203a 2022686a 686a222c 0a202020 2022696d 61676544 61746122 203a2022 72617720 64617461 222c0a20 20202022 65787069 72617469 6f6e5469 6d652220 3a202231 33333935 33383430 30303030 222c0a20 20202022 6576656e 744c6f63 6174696f 6e22203a 207b0a20 20202020 20226c6f 6e676974 75646522 203a2022 2d332e34 36222c0a 20202020 2020226c 61746974 75646522 203a2022 34332e39 33383338 33383322 0a202020 207d2c0a 20202020 22657665 6e744461 74652220 3a20224a 756e2031 332c2032 30313220 31323a30 303a3030 20414d22 0a20207d 2c0a2020 22747970 6522203a 2022454c 44494152 494f4d4f 4e54414e 4553222c 0a202022 7469746c 6522203a 20226163 63494445 4e544522 0a7d>
What is this exactly?? How can i just convert that dictionary to look exactly like this:
这究竟是什么??我如何才能将该字典转换为完全像这样:
{
"eventData": {
"eventDate": "Jun 13, 2012 12:00:00 AM",
"eventLocation": {
"latitude": 43.93838383,
"longitude": -3.46
},
"text": "hjhj",
"imageData": "raw data",
"imageFormat": "JPEG",
"expirationTime": 1339538400000
},
"type": "ELDIARIOMONTANES",
"title": "accIDENTE"
}
If possible i want the above format to be an NSString cause that was what i was sending to the server and it was working. I mean an NSString , in json format like this. Thank you very much for bothering reading all this! :D
如果可能的话,我希望上述格式是一个 NSString 原因,这是我发送到服务器的内容并且它正在工作。我的意思是一个 NSString ,像这样的 json 格式。非常感谢您费心阅读这一切!:D
回答by Antonio MG
Your conversion is fine, NSData looks like that because it's a bunch of bytes!
您的转换很好, NSData 看起来像那样,因为它是一堆字节!
You need to convert it to string, try this:
您需要将其转换为字符串,试试这个:
NSString* aStr;
aStr = [[NSString alloc] initWithData:aData encoding:NSUTF8StringEncoding];
The method:
方法:
+ (NSData *)dataWithJSONObject:(id)obj options:(NSJSONWritingOptions)opt error:(NSError **)error
Returns a NSData object, NSData is just an object wrapper for a byte representation, so your dictionary is there, but in the form of a byte representation.
返回一个 NSData 对象,NSData 只是一个字节表示的对象包装器,所以你的字典就在那里,但是以字节表示的形式。
Then you use:
然后你使用:
- (instancetype)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding
This method takes your byte representation and turn it into a string, using the encoding that you provide.
此方法使用您提供的编码将您的字节表示形式转换为字符串。