如何将 Java Hashtable 转换为 NSDictionary (obj-C)?
在服务器端(GAE),我有一个java哈希表。
在客户端(iPhone),我试图创建一个 NSDictionary。
myHashTable.toString() 给我提供了一些看起来与 [myDictionary 描述] 非常接近但不完全相同的东西。如果它们相同,我可以将字符串写入文件并执行以下操作:
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:tmpFile];
我可以在 obj-C 中编写一个小解析器来处理 myHashtable.toString(),但我有点希望某个地方已经内置了一条捷径——我似乎找不到它。
(所以,作为一个极客,我在网络上搜索快捷方式的时间远远比我编写和调试解析器花费的时间要长……;)
无论如何 - 提示?
谢谢!
At the server end (GAE), I've got a java Hashtable.
At the client end (iPhone), I'm trying to create an NSDictionary.
myHashTable.toString() gets me something that looks darned-close-to-but-not-quite-the-same-as [myDictionary description]. If they were the same, I could write the string to a file and do:
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:tmpFile];
I could write a little parser in obj-C to deal with myHashtable.toString(), but I'm sort-of hoping that there's a shortcut already built into something, somewhere -- I just can't seem to find it.
(So, being a geek, I'll spend far longer searching the web for a shortcut than it would take me to write & debug the parser... ;)
Anyway -- hints?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会将
Hashtable
转换为类似JSON
的内容,并将其放在 iPhone 端。Hashtable.toString()
并不理想,它会出现空格、逗号和引号的问题。对于 JSON 到 NSDictionary,您可以在 http:// 下找到
json-framework
工具www.json.org/I would convert the
Hashtable
into somethingJSON
-like and take it on the iPhone side.Hashtable.toString()
is not ideal, it will have problem with spaces, comma and quotation marks.For JSON-to-NSDictionary, you can find the
json-framework
tools under http://www.json.org/正如 j-16 SDiZ 提到的,您需要序列化哈希表。它可以是 json、xml 或其他格式。序列化后,您需要将它们反序列化为 NSDictionary。 JSON 可能是最简单的格式,有大量适用于 Objective-C 和 Java 的库。 http://json.org 有一个库列表。
As j-16 SDiZ mentioned, you need to serialize your hashtable. It can be to json, xml or some other format. Once serialized, you need to deserialize them into an NSDictionary. JSON is probably the easiest format to do this with plenty of libraries for both Objective-C and Java. http://json.org has a list of libraries.