Eugene Eugene - 18 days ago 8
JSON Question

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.

Answer

If you're trying to post OrdersList 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);
Comments