如何从URL获取json字符串?

时间:2014-03-21 05:15:25

标签: c# asp.net json visual-studio-2010

网址为:http://reportguru.webdenza.com/vdetect-pro-2/api.php?q= {%22svc%22:%22avl_evts%22,%22app%22:%22hst%22,%22sid%22:%22e93c3c3fbc1e3add3a518ca9d3f28d65%22}

url包含以下json字符串。

{
"tm": 1395378731,
"events": [
    {
        "i": 828930,
        "t": "u",
        "d": {
            "prms": {
                "battery_charge": {
                    "v": 0,
                    "ct": 1395374632,
                    "at": 1395374632
                },
                "in": {
                    "v": 0,
                    "ct": 1395374632,
                    "at": 1395374632
                },
                "in1": {
                    "v": 0,
                    "ct": 1395374632,
                    "at": 1395374632
                },
                "in10": 1395374632,
                "in11": 1395374632,
                "in12": 1395374632,
                "in13": 1395374632,
                "in14": 1395374632,
                "in15": 1395374632,
                "in16": 1395374632,
                "in17": 1395374632,
                "in18": 1395374632,
                "in19": 1395374632,
                "in2": 1395374632,
                "in20": 1395374632,
                "in21": 1395374632,
                "in22": 1395374632,
                "in23": 1395374632,
                "in24": 1395374632,
                "in25": 1395374632,
                "in26": 1395374632,
                "in27": 1395374632,
                "in28": 1395374632,
                "in29": 1395374632,
                "in3": 1395374632,
                "in30": 1395374632,
                "in31": 1395374632,
                "in32": 1395374632,
                "in4": 1395374632,
                "in5": 1395374632,
                "in6": 1395374632,
                "in7": 1395374632,
                "in8": 1395374632,
                "in9": 1395374632,
                "param179": 1395374632,
                "param180": 1395374632,
                "param250": {
                    "v": 0,
                    "ct": 1395374632,
                    "at": 1395374632
                },
                "posinfo": {
                    "v": {
                        "y": 24.9639728,
                        "x": 55.202432,
                        "z": 57,
                        "c": 0,
                        "sc": 9
                    },
                    "ct": 1395374632,
                    "at": 1395374632
                },
                "speed": {
                    "v": 0,
                    "ct": 1395374632,
                    "at": 1395374632
                }
            }
        }
    },
    {
        "i": 828930,
        "t": "m",
        "d": {
            "t": 1395374507,
            "f": 3,
            "tp": "ud",
            "pos": {
                "y": 24.9655744,
                "x": 55.2024064,
                "z": 25,
                "s": 7,
                "c": 164,
                "sc": 10
            },
            "i": 1,
            "p": {
                "battery_charge": 1,
                "param179": 0,
                "param180": 0
            }
        }
    },
    {
        "i": 828930,
        "t": "u",
        "d": {
            "prms": {
                "battery_charge": {
                    "v": 0,
                    "ct": 1395374632,
                    "at": 1395374632
                },
                "in": {
                    "v": 0,
                    "ct": 1395374632,
                    "at": 1395374632
                },
                "in1": {
                    "v": 0,
                    "ct": 1395374632,
                    "at": 1395374632
                },
                "in10": 1395374632,
                "in11": 1395374632,
                "in12": 1395374632,
                "in13": 1395374632,
                "in14": 1395374632,
                "in15": 1395374632,
                "in16": 1395374632,
                "in17": 1395374632,
                "in18": 1395374632,
                "in19": 1395374632,
                "in2": 1395374632,
                "in20": 1395374632,
                "in21": 1395374632,
                "in22": 1395374632,
                "in23": 1395374632,
                "in24": 1395374632,
                "in25": 1395374632,
                "in26": 1395374632,
                "in27": 1395374632,
                "in28": 1395374632,
                "in29": 1395374632,
                "in3": 1395374632,
                "in30": 1395374632,
                "in31": 1395374632,
                "in32": 1395374632,
                "in4": 1395374632,
                "in5": 1395374632,
                "in6": 1395374632,
                "in7": 1395374632,
                "in8": 1395374632,
                "in9": 1395374632,
                "param179": 1395374632,
                "param180": 1395374632,
                "param250": {
                    "v": 0,
                    "ct": 1395374632,
                    "at": 1395374632
                },
                "posinfo": {
                    "v": {
                        "y": 24.9639728,
                        "x": 55.202432,
                        "z": 57,
                        "c": 0,
                        "sc": 9
                    },
                    "ct": 1395374632,
                    "at": 1395374632
                },
                "speed": {
                    "v": 0,
                    "ct": 1395374632,
                    "at": 1395374632
                }
            }
        }
    },
    {
        "i": 828930,
        "t": "m",
        "d": {
            "t": 1395374504,
            "f": 3,
            "tp": "ud",
            "pos": {
                "y": 24.9656512,
                "x": 55.2023744,
                "z": 24,
                "s": 17,
                "c": 130,
                "sc": 10
            },
            "i": 1,
            "p": {
                "battery_charge": 1,
                "param179": 0,
                "param180": 0
            }
        }
    },
    {
        "i": 828930,
        "t": "u",
        "d": {
            "prms": {
                "battery_charge": {
                    "v": 0,
                    "ct": 1395374632,
                    "at": 1395374632
                },
                "in": {
                    "v": 0,
                    "ct": 1395374632,
                    "at": 1395374632
                },
                "in1": {
                    "v": 0,
                    "ct": 1395374632,
                    "at": 1395374632
                },
                "in10": 1395374632,
                "in11": 1395374632,
                "in12": 1395374632,
                "in13": 1395374632,
                "in14": 1395374632,
                "in15": 1395374632,
                "in16": 1395374632,
                "in17": 1395374632,
                "in18": 1395374632,
                "in19": 1395374632,
                "in2": 1395374632,
                "in20": 1395374632,
                "in21": 1395374632,
                "in22": 1395374632,
                "in23": 1395374632,
                "in24": 1395374632,
                "in25": 1395374632,
                "in26": 1395374632,
                "in27": 1395374632,
                "in28": 1395374632,
                "in29": 1395374632,
                "in3": 1395374632,
                "in30": 1395374632,
                "in31": 1395374632,
                "in32": 1395374632,
                "in4": 1395374632,
                "in5": 1395374632,
                "in6": 1395374632,
                "in7": 1395374632,
                "in8": 1395374632,
                "in9": 1395374632,
                "param179": 1395374632,
                "param180": 1395374632,
                "param250": {
                    "v": 0,
                    "ct": 1395374632,
                    "at": 1395374632
                },
                "posinfo": {
                    "v": {
                        "y": 24.9639728,
                        "x": 55.202432,
                        "z": 57,
                        "c": 0,
                        "sc": 9
                    },
                    "ct": 1395374632,
                    "at": 1395374632
                },
                "speed": {
                    "v": 0,
                    "ct": 1395374632,
                    "at": 1395374632
                }
            }
        }
    },
    {
        "i": 828930,
        "t": "m",
        "d": {
            "t": 1395374632,
            "f": 3,
            "tp": "ud",
            "pos": {
                "y": 24.9639728,
                "x": 55.202432,
                "z": 57,
                "s": 0,
                "c": 0,
                "sc": 9
            },
            "i": 0,
            "p": {
                "param179": 0,
                "param180": 0,
                "param250": 0,
                "battery_charge": 0
            }
        }
    }
]
}

请帮助我如何获取字符串并获取Asp.net(c#)中的数据。

5 个答案:

答案 0 :(得分:3)

C#是使用此类提供System.Net获取字符串的WebClient类,如

var json = new WebClient().DownloadString("url");

答案 1 :(得分:1)

您可以使用JavaScript Serializer。

请参阅以下问题解答。 Parsing JSON using Json.net

答案 2 :(得分:1)

来自url的

文本可以使用WebClient读取。可以使用javascriptSerializer将此结果更改为对象表示法。

答案 3 :(得分:1)

您需要将任务分解为步骤,。

首先以字符串格式获取网址

string url = HttpContext.Current.Request.Url.AbsoluteUri;

然后,您需要提取JSON格式的数据并将其放入文件(保存)

File.WriteAllText(@" c:\ url.json",url);

您可能必须使用Newtonsoft JSON.Net框架对其进行序列化。但是,我相信这是它的主旨。

答案 4 :(得分:1)

从网址获取字符串

string Strjson = new WebClient().DownloadString(UrlQuery);

将json字符串转换为Class Object

 ClsObject = (ClsObject) Newtonsoft.Json.JsonConvert.DeserializeObject(Strjson);

ClsObject = Newtonsoft.Json.JsonConvert.DeserializeObject(Strjson,ClsObject);

需要将Newtonsoft库添加到项目中

下载Nuget