Question:
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 else I need to put the words #acimadopeso and #diabetes in a List
. how can I do this?
Answer:
As this is a JSON I recommend using the Json.NET Newtonsoft library to get the data you want.
Just parse it using the JArray
class and in a foreach
get the key tag
values so you can fill your string list, for 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());
}
}
Output tags.ForEach(x => Console.WriteLine(x));
#acimadopeso
#diabetes
#idoso
See working in .NET Fiddle .