Doubt with string, extract data

Asked

Viewed 64 times

3

I have the following string

[{"tipo":"simnao","pergunta":"rwerwerwerewr ?","tag":"#acimadopeso","resposta":"","$$hashkey":"object:410"},{"pergunta":"werwerwerwer ?","tag":"#diabetes","resposta":"","$$hashkey":"object:412"},{"pergunta":"werwerwer ?","tag":"#idoso","resposta":"","$$hashkey":"object:414"}]

I need to extract to a list only the words that contain the "#", or need to play in a List the words #greyingweight and #diabetes. how can I do that?

2 answers

5

Since it is a JSON I recommend using the library Json.NET Newtonsoft to get the data you want.

Just do a Parsing using the class JArray and in a foreach get the key values tag so that you can fill in your string list, example:

using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;

public class Program
{
    public static void Main()
    {
        var json = "[{\"tipo\":\"simnao\",\"pergunta\":\"rwerw#erwerewr ?\",\"tag\":\"#acimadopeso\",\"resposta\":\"\",\"$$hashkey\":\"object:410\"},{\"pergunta\":\"werwerwerwer ?\",\"tag\":\"#diabetes\",\"resposta\":\"\",\"$$hashkey\":\"object:412\"},{\"pergunta\":\"werwerwer ?\",\"tag\":\"#idoso\",\"resposta\":\"\",\"$$hashkey\":\"object:414\"}]";     
        JArray array = JArray.Parse(json);      
        var tags = new List<string>();      
        foreach (var a in array)
            tags.Add(a["tag"].ToString());      
    }
}

Exit tags.ForEach(x => Console.WriteLine(x));

#acimadopeso
#diabetes
#idoso

See working on .NET Fiddle.

3


Search the words it contains #

You can do this way, the explanation is in the code comments

using System;
using System.Linq;
                    
public class Program
{
    public static void Main()
    {
        string stringJson = "[{\"tipo\":\"simnao\",\"pergunta\":\"rwerw#erwerewr ?\",\"tag\":\"#acimadopeso\",\"resposta\":\"\",\"$$hashkey\":\"object:410\"},{\"pergunta\":\"werwerwerwer ?\",\"tag\":\"#diabetes\",\"resposta\":\"\",\"$$hashkey\":\"object:412\"},{\"pergunta\":\"werwerwer ?\",\"tag\":\"#idoso\",\"resposta\":\"\",\"$$hashkey\":\"object:414\"}]";
        
        //Separa a string por aspas 
        string[] arrayString = stringJson.Split('\"');
        
        //Procura as strings que contenha "#"
        var listString = arrayString.Where(x => x.Contains("#")).ToList();
        
        //Mostra o resultado
        listString.ForEach(i => Console.Write("{0}\n", i));
    }
}

Execute

Search the words that begin with #

In the question you said you wanted the words that contain #, but I believe you want the words that begin with #

using System;
using System.Linq;
                
public class Program
{
    public static void Main()
    {
        string stringJson = "[{\"tipo\":\"simnao\",\"pergunta\":\"rwerw#erwerewr ?\",\"tag\":\"#acimadopeso\",\"resposta\":\"\",\"$$hashkey\":\"object:410\"},{\"pergunta\":\"werwerwerwer ?\",\"tag\":\"#diabetes\",\"resposta\":\"\",\"$$hashkey\":\"object:412\"},{\"pergunta\":\"werwerwer ?\",\"tag\":\"#idoso\",\"resposta\":\"\",\"$$hashkey\":\"object:414\"}]";
        
        //Separa a string por aspas 
        string[] arrayString = stringJson.Split('\"');
        
        //Procura pelas strings que começam com "#"
        var listString = arrayString.Where(x => x.FirstOrDefault() == '#').ToList();
        
        //Mostra o resultado
        listString.ForEach(i => Console.Write("{0}\n", i));
    }
}

Execute

Browser other questions tagged

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