C # using structs to get JSON data

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.