简体   繁体   中英

Get value from property and serialize it

I writing app for UWP.

I have this code

private List<RootObject> ordersList;

    public List<RootObject> OrdersList
    {
        get { return ordersList; }
        set
        {
            ordersList = value;
            OnPropertyChanged();
        }
    }


    private RootObject ordersChange;

    public RootObject OrdersChange
    {
        get { return ordersChange; }
        set
        {
            ordersChange = value;
            OnPropertyChanged();
        }
    }

When I click button value write to ordersChange and set to value.

I need to take value, serialize and send via post request.

  using (HttpClient httpClient = new HttpClient())
        {
            httpClient.BaseAddress = new Uri(@"http://api.simplegames.com.ua");
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            httpClient.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("utf-8"));

            string endpoint = @"/post_from_local.php";

            try
            {
                HttpContent content = new StringContent(JsonConvert.SerializeObject(*****), Encoding.UTF8, "application/json");
                HttpResponseMessage response = await httpClient.PostAsync(endpoint, content);

Where *** is value. How I can do this?

Thank's for help.

If you're trying to post OrdersChange you can do like this.

var json = JsonConvert.SerializeObject(viewModelInstance.OrdersChange);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response  = await client.PostAsync(apiUrl, content);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM