-4

some ideas how to read this simple JSON String to a List?

["amazon.de","ebay.de","fischevortische.de","homefuerst.de"]

i just wanna add every item to my List blackList

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364

3 Answers3

1

You have two solutions:

  1. Quick but only for a list of string.

    string jsonText = "[\"amazon.de\",\"ebay.de\",\"fischevortische.de\",\"homefuerst.de\"]";
    char[] separators = new char[] {'[', ']', ',', '"'};
    string[] result1 = jsonText.Split(separators, StringSplitOptions.RemoveEmptyEntries);
    
  2. Using JSON library (included in .NET)

    string jsonText = "[\"amazon.de\",\"ebay.de\",\"fischevortische.de\",\"homefuerst.de\"]";
    var jss = new JavaScriptSerializer();
    var result2 = jss.Deserialize<string[]>(jsonText);
    
Michael Kohne
  • 11,888
  • 3
  • 47
  • 79
Denis Voituron
  • 288
  • 1
  • 8
0
string json = "[\"amazon.de\",\"ebay.de\",\"fischevortische.de\",\"homefuerst.de\"]";
List<string> parsed = Newtonsoft.Json.JsonConvert.DeserializeObject<Newtonsoft.Json.Linq.JArray>(json).Values<string>().ToList();
General-Doomer
  • 2,681
  • 13
  • 13
0
var result = JsonConvert.DeserializeObject<List<string>>();