JSON 排序字符串

发布于 2024-12-05 04:57:32 字数 1644 浏览 0 评论 0原文

框架。 你好,我正在使用 json 框架。

使用 JSONRepresentation 方法时如何获取排序后的字符串? 这是我的代码:

NSMutableDictionary *tUserIDParam = [NSMutableDictionary dictionary];

[tUserIDParam setObject:@"UserID" forKey:@"@key"];
[tUserIDParam setObject:@"string" forKey:@"@type"];
[tUserIDParam setObject:@"mark" forKey:@"#text"];

NSMutableDictionary *tPasswordParam = [NSMutableDictionary dictionary];
[tPasswordParam setObject:@"Password" forKey:@"@key"];
[tPasswordParam setObject:@"string" forKey:@"@type"];
[tPasswordParam setObject:@"123456" forKey:@"#text"];

NSArray *tParams = [NSArray arrayWithObjects:tUserIDParam,tPasswordParam, nil];
NSDictionary *tParam = [NSDictionary dictionaryWithObject:tParams forKey:@"param"];

NSMutableDictionary *tRequests = [NSMutableDictionary dictionary];
[tRequests setObject:[NSNull null] forKey:@"key"];
[tRequests setObject:[NSNull null] forKey:@"basic"];
[tRequests setObject:tParam forKey:@"conditions"];

NSDictionary *tRequest = [NSDictionary dictionaryWithObject:tRequests forKey:@"request"];

NSString *tJSONStrFromDict = [tRequest JSONRepresentation];

我想获取 JSON 字符串,例如:

{"request":{"key":null,"basic":null,"conditions":{"param":[{"@key":"UserID","@type":"string","#text ":"mark"},{"@key":"Password","@type":"string","#text":"123456"}]}}}

但是我从上面的代码中得到了这个字符串:

{“请求”:{“基本”:空,“条件”: {"param":[{"#text":"mark","@key":"用户ID","@type":"string"},{"#text":"123456","@key": "Password","@type":"string"}]},"key":null}}

我尝试使用 SBJSONWriter sortKeys 属性,但它不起作用。 ..

有什么办法可以解决我的问题吗?

谢谢!

framework.
Hi I'm using json-framework.

How to get sorted string when using JSONRepresentation method?
Here is my code:

NSMutableDictionary *tUserIDParam = [NSMutableDictionary dictionary];

[tUserIDParam setObject:@"UserID" forKey:@"@key"];
[tUserIDParam setObject:@"string" forKey:@"@type"];
[tUserIDParam setObject:@"mark" forKey:@"#text"];

NSMutableDictionary *tPasswordParam = [NSMutableDictionary dictionary];
[tPasswordParam setObject:@"Password" forKey:@"@key"];
[tPasswordParam setObject:@"string" forKey:@"@type"];
[tPasswordParam setObject:@"123456" forKey:@"#text"];

NSArray *tParams = [NSArray arrayWithObjects:tUserIDParam,tPasswordParam, nil];
NSDictionary *tParam = [NSDictionary dictionaryWithObject:tParams forKey:@"param"];

NSMutableDictionary *tRequests = [NSMutableDictionary dictionary];
[tRequests setObject:[NSNull null] forKey:@"key"];
[tRequests setObject:[NSNull null] forKey:@"basic"];
[tRequests setObject:tParam forKey:@"conditions"];

NSDictionary *tRequest = [NSDictionary dictionaryWithObject:tRequests forKey:@"request"];

NSString *tJSONStrFromDict = [tRequest JSONRepresentation];

I want to get JSON string like:

{"request":{"key":null,"basic":null,"conditions":{"param":[{"@key":"UserID","@type":"string","#text":"mark"},{"@key":"Password","@type":"string","#text":"123456"}]}}}

But I got this string from above code:

{"request":{"basic":null,"conditions":
{"param":[{"#text":"mark","@key":"UserID","@type":"string"},{"#text":"123456","@key":"Password","@type":"string"}]},"key":null}}

I try to use SBJSONWriter sortKeys property,it does not work...

Have any way to solve my problem?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔少女心 2024-12-12 04:57:32

NSMutableDictionary 实例不保留元素的顺序,无论是按字母顺序还是按插入顺序。这与 JSON 无关。

此外,JSON 的对象数据模型也不保证顺序。如果您想要特定的顺序,则必须对有序部分使用 JSON 数组。

BTW.: SBJSONWriter sortKeys 的作用是输出按键名称字母顺序排序的对象元素(或属性)。这就是你得到的。

NSMutableDictionary instance do not preserve the order of elements, neither in alphabeticallz order nor in the order they were inserted. This is independent of JSON.

Furthermore, JSON's data model for object doesn't guarantee order either. If you want a certain order, you'll have to use JSON arrays for the ordered parts.

BTW.: The effect of SBJSONWriter sortKeys is to output the object elements (or properties) alphabetically sorted by the key name. This is what you get.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文