简体   繁体   English

c#如何将Json数据检索到数组中

[英]c# How to retrieve Json data into an array

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”:{“姓名”:“John”,“电话”:[“Mob - 98837374”,“Mob - 98363627”]},“002”:{“姓名”:“Tom”,“电话”: [“Mob - 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“

Have a look at this: Parsing JSON using Json.net 看看这个: 使用Json.net解析JSON

Here is some Json.NET specific documentation for serializing and deserializing arrays: Serializing Collections with Json.NET 下面是一些用于序列化和反序列化数组的Json.NET特定文档: 使用Json.NET序列化集合

出于什么目的你将使用它...如果你在客户端使用它使用相同的json数据,因为它将是巨大的性能但如果你需要它在服务器端然后搜索转换json字符串的替代方法到数组列表 ....

The JSON structure as it stands has some problems. 现有的JSON结构存在一些问题。 It appears to be using property names such as "001" and "002" as data. 它似乎使用诸如“001”和“002”的属性名称作为数据。 This is fine in Javascript but its real tricky to handle in C#. 这在Javascript中很好,但在C#中处理它真的很棘手。 A better stucture would be:- 更好的结构是: -

[ {"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可以更有效地使用。

Since i am developing for a windows mobile device, it seems like Json.net is too large. 由于我正在为Windows移动设备开发,看起来Json.net太大了。 I get an error when trying to debug, and i see that the whole device's memory is consumed. 我在尝试调试时遇到错误,我发现整个设备的内存已被占用。

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溢出的人很棒!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM