I have a JArray like this:
jaFruits = ["Apple","Pineapple","Banana","Orange"];
How to convert it into a string like below?
string strFruits -> "'Apple', 'Pineapple', 'Banana', 'Orange'";
any help is appreciated.
thank you so much in advance.
Regards Don
CodePudding user response:
I'd do something along these lines:
string strFruits = String.Join(", ",jaFruits.Select(m=>$"'{m}'"));
CodePudding user response:
string[] fruits= jaFruits.ToObject<string[]>();
var strFruits = String.Join(",", fruits);
CodePudding user response:
By using JsonConvert.DeserializeObject Method in order to extract the values/string from JArray and then loop the object to get the string you expected.
Example:
static void Main(string[] args)
{
string ja = @"{""Col"": [ { ""Text"": ""select1"", ""Val"": ""select1"" }, { ""Text"": ""lastname"", ""Val"": ""lastname"" }]}";
A js = JsonConvert.DeserializeObject<A>(ja);
string s = "";
foreach (var i in js.Col)
{
s = i.Val ";";
}
Console.Write(s.Substring(0, s.Length - 1));
}
public class A
{
public List<B> Col { get; set; }
}
public class B
{
public string Text { get; set; }
public string Val { get; set; }
}
Credits/reference @User-719153870 : Microsoft Social Post