Recently made payment, to get the user's OpenID, call the interface after the return of the JSON format of data, I want to in C # backstage to take out the data, check online, find the following methods:
1. First call the interface, to have a post data to the specified URL and return the data function:
protected string Postxmltourl (stringstring postdata) { string""; using New System.Net.WebClient ()) { "POST", PostData); } return returnmsg; }
The data format of the post can be
URL parameter format (a=1&b=2&c=3 ...)
XML Format (<xml>....</xml>)
The data format returned is determined by the interface.
2. Method invocation:
string " appid= " " &secret= " " &code= " " &grant_type=authorization_code " ; string requestData = Postxmltourl ("Https://api.weixin.qq.com/sns/oauth2/access_token ", Post_data);
The data returned by this interface is in JSON format, so the resulting requestdata is a JSON string:
{ "Access_token":"Access_token", "expires_in":7200, "Refresh_token":"Refresh_token", "OpenID":"OPENID", "Scope":"SCOPE"}
The returned data contains the OpenID I need, and the next step is to use the struct to remove the OpenID. First define the structure body:
Public structAuthorization { Public stringAccess_token {Get;Set; }//The name of the property must be the same as the "key" value in the JSON format string. Public stringexpires_in {Get;Set; } Public stringRefresh_token {Get;Set; } Public stringOpenID {Get;Set; } Public stringScope {Get;Set; } }
Convert JSON data to an object type using the class JavaScriptSerializer of the serialized data:
New JavaScriptSerializer (); // instantiate a class that can serialize data Authorization auth = js. Deserialize<authorization> (requestData); // Convert JSON data to object type and assign value to auth
Such a transformation, the object auth inside the 4 attributes will be the corresponding value, so I get the interface returned to the OpenID, you can use it where needed:
Wxobpay.openid = Auth.openid;
C # using structs to get JSON data