Turn a string into json

Asked

Viewed 881 times

-6

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

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

At 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 you get to this line

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

the system aborts. Nor enters the catch.

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

  • Includes your string

  • Please, if possible, improve the description of your problem, and also supplement it with what you have already done, or mistakes that are happening to help us help you.

  • In reality is to mount a json with this output. I have no idea how I would do with the string. If string js="Matriz:12,Filial:21. I have no idea what it would be like, reason for the post.

  • You have this data as in C#? The . net has object serializers for json.

1 answer

1


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

To describe it as a string you only 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 exhaust code \" to indicate double quotes.

  • I was doing it, but I wasn’t putting the character "". But I didn’t know about the simple quote. Nor did it cross my mind. Vaçleu Onosendai. As soon as it runs and works, I mark your answer. And then I serializo, as I do to generate the json?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.