Turning a string into json

-6

I need to mount this json from a string and pass it as a parameter:

I did this: string s = "{\"Matriz\":12, \"Filial\":21}";

In the click of my button I have this:

private void Click_Service(object sender, EventArgs e)
        {
            DataService dataService = new DataService();
            string jvalue = "{"\"Matriz\":12, \"Filial\":21"}";
            dataService.PostIndicador(jvalue);
        }

And in my service I have this:

public async Task<string> PostIndicador(string jsonValue)
        {
            string retorno = null;
            //string jvalue = JsonConvert.SerializeObject(jsonValue);
            if (NetworkCheck.IsInternet())
            {
                string url_base = $"meu_ip";
                var uri = new Uri(string.Format(url_base));
                using (var client = new HttpClient() { BaseAddress = uri })
                {
                    var responeMessage =
                    await client.PostAsync("/Service/Service.svc/GetIndicator", new StringContent("jsonValue", Encoding.UTF8, "application/json"));
                    var resultcontent = await responeMessage.Content.ReadAsStringAsync();
                    retorno = (JsonConvert.DeserializeObject(resultcontent)).ToString();
                }                
            }

            return retorno;
        }

The whole question is whether jsonValue is in the correct format, because when it arrives at this line

var responeMessage = await client.PostAsync("/Service/Service.svc/GetIndicator", new StringContent("jsonValue", Encoding.UTF8, "application/json"));

The system aborts. Nor does it enter catch .

I hope that with this edit I left the original question, I think not since the goal is to mount json correctly.

    
asked by anonymous 11.01.2018 / 17:27

1 answer

1

JSON, which in free translation of JavaScript Object Notation means javascript object notation , is by itself a serialized representation of an object.

To describe it as a string you just need to format the notation using the rules of your chosen language - in this case, C #:

string json = "{\"Matriz\": 12, \"Filial\": 21 }";

The only difference is the use of the escape code \" to indicate double quotes.

    
11.01.2018 / 17:57