0

几周前我开始用 C# 编程工作,我试图从大量数据中提取一些信息,这些数据实际上是通过 GET/POST http 请求收集的。

到目前为止,我对整体没有任何问题,但我现在正在努力解决这个困难,我相信在你看来这似乎很荒谬。这就是我向你走来的原因,不用担心,我试图研究它但没有成功。

这是我的清单

[{"id":"28",
"name":"Whitechapel Station",
"logo":"gfx\/whitechapel_station\/whitechapel_station_logo.gif",
"x":"85129",
"y":"36575",
"extentsX1":"0",
"extentsX2":"0",
"extentsY1":"0",
"extentsY2":"0",
"zoomlevel":"4",
"code":"PRJ",
"asset_type":"fixed",
"symbol":"project"}

,{"id":"46",
"name":"Connaught Tunnel",
"logo":"gfx\/connaught_tunnel\/connaught_tunnel_logo.",
"x":"91668",
"y":"35238",
"extentsX1":"0",
"extentsX2":"0",
"extentsY1":"0",
"extentsY2":"0",
"zoomlevel":"4",
"code":"PRJ",
"asset_type":"fixed",
"symbol":"project"}
]

实际上我的想法是只提取每束大 {} 的“id”和“name”。你有什么想法 ?

欢呼队友。

克莱门特

编辑:代码不像通常这样。它只是一条没有回报的大直线,如下所示。

[{"id":"28","name":"Whitechapel Station","logo":"gfx\/whitechapel_station\/whitechapel_station_logo.gif","x":"85129","y":"36575","extentsX1":"0","extentsX2":"0","extentsY1":"0","extentsY2":"0","zoomlevel":"4","code":"PRJ","asset_type":"fixed","symbol":"project"},{"id":"46","name":"Connaught Tunnel","logo":"gfx\/connaught_tunnel\/connaught_tunnel_logo.","x":"91668","y":"35238","extentsX1":"0","extentsX2":"0","extentsY1":"0","extentsY2":"0","zoomlevel":"4","code":"PRJ","asset_type":"fixed","symbol":"project"}]
4

2 回答 2

4

您发布的示例采用JSON格式。为了反序列化 JSOn 字符串,您应该使用JSON.NET 之类的工具。

首先,制作与要反序列化的属性匹配的 ac# 对象:

public class MyObject
{
    public int id { get; set; }
    public string name { get; set; }
}

然后,反序列化:

var myObjects = JsonConvert.DeserializeObject<List<MyObject>>(theString);

然后,这些值将提供给您:

int id = myObjects[0].id;
string name = myObjects[0].name;
于 2013-04-10T17:15:12.890 回答
0

好的,基本上我犯了一个严重的错误。我在每个 [ 或 ] 和 { 或 } 之间添加了字符“。已解决!

于 2013-04-12T13:15:00.510 回答