c#如何将Json数据检索到数组中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1459510/
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
c# How to retrieve Json data into an array
提问by Ikky
I have found different libraries that can parse Json data, but i did not find any documentation on how to get the data into an C# array or list.
我找到了可以解析 Json 数据的不同库,但我没有找到任何关于如何将数据放入 C# 数组或列表的文档。
I got this Json data:
我得到了这个 Json 数据:
{"001":{"Name":"John", "Phone":["Mob - 98837374","Mob - 98363627"]}, "002":{"Name":"Tom", "Phone":["Mob - 49858857"]}}
{"001":{"Name":"John", "Phone":["Mob - 98837374","Mob - 98363627"]}, "002":{"Name":"Tom", "Phone": [“暴徒 - 49858857”]}}
Anybody got a clue? :)
有人有线索吗?:)
Edit:
编辑:
Useful details posted by OP as comments re-posted as question edit by AnthonyWJones
OP 发布的有用详细信息作为评论重新发布为 AnthonyWJones 的问题编辑
My code using JSON.NET:
我使用 JSON.NET 的代码:
StringBuilder sb = new StringBuilder();
List<string> entities = (List<string>)JsonConvert.DeserializeObject(responseFromServer, typeof(List<string>));
foreach (string items in entities) {
sb.Append(items);
}
But i always get an error when i debug:
但是我调试的时候总是报错:
Warning 1 Reference to type 'System.DateTimeOffset' claims it is defined in 'c:\Program >> Files\Microsoft.NET\SDK\CompactFramework\v3.5\WindowsCE\mscorlib.dll', but it could not >> be found c:\Div\Json dot net\Bin\Newtonsoft.Json.dll"
警告 1 对类型“System.DateTimeOffset”的引用声称它是在“c:\Program >> Files\Microsoft.NET\SDK\CompactFramework\v3.5\WindowsCE\mscorlib.dll”中定义的,但它不能 >> 是找到 c:\Div\Json dot net\Bin\Newtonsoft.Json.dll"
回答by Lazarus
Have a look at this: Parsing JSON using Json.net
看看这个:Parsing JSON using Json.net
Here is some Json.NET specific documentation for serializing and deserializing arrays: Serializing Collections with Json.NET
以下是一些用于序列化和反序列化数组的 Json.NET 特定文档:Serializing Collections with Json.NET
回答by ACP
for what purpose you r going to use it... If you are using it on client side use the same jsondata because it would be huge in performance but if you need it in server side then search for an alternative method which converts json string to array list....
你打算使用它的目的是什么......如果你在客户端使用它,请使用相同的json数据,因为它的性能会很大,但如果你在服务器端需要它,然后搜索转换json字符串的替代方法到数组列表....
回答by AnthonyWJones
The JSON structure as it stands has some problems. It appears to be using property names such as "001" and "002" as data. This is fine in Javascript but its real tricky to handle in C#. A better stucture would be:-
目前的 JSON 结构存在一些问题。它似乎使用诸如“001”和“002”之类的属性名称作为数据。这在 Javascript 中很好,但在 C# 中处理起来确实很棘手。更好的结构是:-
[ {"ID": "001", "Name":"John", "Phone":["Mob - 98837374","Mob - 98363627"]},
{"ID":"002", "Name":"Tom", "Phone":["Mob - 49858857"]} ]
Then JSON.NET as others have pointed to can be used more effectively.
然后可以更有效地使用其他人指出的 JSON.NET。
回答by Ikky
Since i am developing for a windows mobile device, it seems like Json.net is too large. I get an error when trying to debug, and i see that the whole device's memory is consumed.
由于我正在为 Windows 移动设备开发,因此 Json.net 似乎太大了。尝试调试时出现错误,我看到整个设备的内存都被消耗了。
Anyway... i think i'll just parse the Json string myself, as i know what the string will conclude.
无论如何......我想我会自己解析Json字符串,因为我知道字符串会得出什么结论。
Thank you for the suggestions anyway.
无论如何,谢谢你的建议。
People on Stack overflow are great!
Stack overflow 上的人很棒!