how to find a string for json

Asked

Viewed 900 times

3

I have a string in json format.

In this way:

{"IdLead":4186960,"Concessionaria":"Mila - Centro","DadosQualificacao":{"IdEvento":79654,"Qualificacao":1,"Motivo":6,"DescricaoMotivo":"motivo 1234","Ficha":["aaaaaaaa - TESTE","Ação desejada:=123456789 teste 132456789","Data Agendamento Test-Drive:=20/04/2018","Já é Cliente da Marca?=SIM"]},"DadosPessoa":{"Nome":"Guilherme Martins","Email":"[email protected]","DDD":11,"Telefone":948831041,"CpfCnpj":"44436740803","PreferenciaContato":0}}

I need to show this string on my screen, however, it appears on a same line. I would like to know how to create this string to show in the identato format.

3 answers

2

With the JSON.NET library it is possible to create an instance of the class JValue from the string and then show the representation of this object in string again using formatting option that respects indentation.

For example:

using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public class Program
{
    public static void Main()
    {
        var json = "{\"id\": 1, \"nome\": \"LINQ\"}";           
        var beauty = JValue.Parse(json).ToString(Formatting.Indented);
        Console.WriteLine(beauty);
    }
}

See working on . NET Fiddle

2


I don’t think it’s worth you trying to build a parser by yourself. There are many things to deal with and you already have it ready.

You can create a method that uses the library’s Reader and Writer Newtonsoft and do the indented rewrite:

public class JsonFormatting
{
    public static string Ident(string json)
    {
        using (var sr = new StringReader(json))
            using (var sw = new StringWriter())
            {
                var jr = new JsonTextReader(sr);
                var jw = new JsonTextWriter(sw) { Formatting = Formatting.Indented };
                jw.WriteToken(jr);
                return sw.ToString();
            }
    }
}

To use it, you will do so:

string json = "{ \"Id\":123456, \"Content\":\"Seu json vai aqui...\"}";
string formatted = JsonFormatting.Ident(json);

This example is available on dotnetfiddle.

I hope I’ve helped.

0

You can use the library JSON.Net

Example of the site itself how it would be:

Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Sizes = new string[] { "Small" };

string json = JsonConvert.SerializeObject(product);
// {
//   "Name": "Apple",
//   "Expiry": "2008-12-28T00:00:00",
//   "Sizes": [
//     "Small"
//   ]
// }

To find only the string you can use:

JsonConvert.SerializeObject(new { 'sua_string_aqui' });
  • So Leticia, I don’t have an object, I have a string already with the JSON pattern, but in my HTML I want to show this string in this indented format

  • @Guilhermenunes use a tag <pre> which says that the text within it already has a format to be shown.

  • @Leticia: So it didn’t roll; It thus formats a json object, and if you pass a string it just add a / between the data.

  • @Lucas, it seems so cool, but he breaks the line where there is space, and this my stirng has no space between the data...

  • @Guillhermenunes is probably not a space he has but a \n (line breaking). You can test with meuJson = meuJson.Replace("\n","")

  • On the JSON.Net website I put the link you can find several examples of how to do. But here is the direct link to the examples: https://www.newtonsoft.com/json/help/html/SerializeObject.htm

Show 1 more comment

Browser other questions tagged

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