0

我在 C# 中工作。我想在我的应用程序中解析 JSON。但不知道如何解析 Json。我在谷歌上搜索了很多但找不到足够的材料。我的 JSon 是..

[{"id":"267","name":"Macmillan Nursing Service","telephone":"01595 743347",
"addr1":"Gilbert Bain Hospital","town":"Shetland","postcode":"ZE1 0TB",
"lat":"60.1505513","lng":"-1.1546642","distance":"3278.42778426762"},

{"id":"355","name":"Orkney Macmillan House","telephone":"01856 888249",
"addr1":"Balfour Hosptial","town":"","postcode":"KW15 1BH","lat":"58.9767143",
"lng":"-2.9649377","distance":"3421.52913012431"},

{"id":"57",
"name":"Community Macmillan Nurse","telephone":"01779 482568",
"addr1":"Peterhead Health    Centre","town":"Peterhead",
"postcode":"AB422XA","lat":"57.5034159","lng":"-1.7939854",
"distance":"3425.64696427705"},

{"id":"287","name":"Macmillan
Service","telephone":"01955 880397","addr1":"Caithness
General Hospital","town":"Wick","postcode":"KW1 5NS","lat":"58.4415",
"lng":"-3.09651","distance":"3452.34795736445"},

{"id":"288","name":"Macmillan Service",
"telephone":"01888 564015","addr1":"The Health Centre","town":"Banff","postcode":"AB45  
1HY","lat":"57.6686963","lng":"-2.5256111","distance":"3457.45181828202"}]

我被困在这项工作中,请帮助我..

任何帮助都会很棒。或者关于如何解析 Json 的一点线索也足够了。

4

2 回答 2

3

使用出色的Json.net库。

特征

Flexible JSON serializer for converting between .NET objects and JSON
LINQ to JSON for manually reading and writing JSON
High performance, faster than .NET's built-in JSON serializers
Write indented, easy to read JSON
Convert JSON to and from XML
Supports .NET 2, .NET 3.5, .NET 4, Silverlight and Windows Phone

当您正在读取或写入的 JSON 映射到 .NET 类时,序列化程序是一个不错的选择。

LINQ to JSON 适用于您只对从 JSON 获取值感兴趣、没有要序列化或反序列化到的类、或者 JSON 与您的类完全不同并且您需要从您的类中手动读取和写入的情况对象。

于 2012-12-20T11:21:22.680 回答
2

似乎您需要将 JSON 字符串反序列化为对象。您可以创建一个代表对象的类,然后将 json 反序列化为它。

就像是

class Information
{
   int id;
   string name;
   string telephone;
   // etc
}

然后用这样的东西反序列化

JavaScriptSerializer json_serializer = new JavaScriptSerializer();
// Using List<T> as the JSON is organised like an array
List<Information> info = (List<Information >)json_serializer.DeserializeObject(JSONFeed);

JSONFeed网站上的 json 字符串在哪里。

有关更深入的方法,请参阅 -代码项目教程

于 2012-12-20T11:28:39.160 回答