How to get json string from the url?

爱⌒轻易说出口 提交于 2021-02-08 06:51:32

问题


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

The url contains the following json string.

{
"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
            }
        }
    }
]
}

Please help me how will I get the string and get the data in Asp.net(c#).


回答1:


C# is provide WebClient class in System.Net get string using this class like

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



回答2:


You can use JavaScript Serializer for this.

See following question answer. Parsing JSON using Json.net




回答3:


text from url can be read using WebClient. This result can be changed to object notation using javascriptSerializer.




回答4:


You need to break the task down into steps,.

First get the URL in string format

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

Then you need to pull the JSON formatted data and place it into a file (save)

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

You might have to serialize it using the Newtonsoft JSON.Net framework. But, I believe that's the jist of it.




回答5:


get string from url

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

convert json string to Class Object

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

or

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

Needs to add the Newtonsoft library to your project

Download Nuget



来源:https://stackoverflow.com/questions/22550738/how-to-get-json-string-from-the-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!