Share via


How can i get the values from a JArray to form a string ?

Question

Monday, November 11, 2019 3:50 AM

 Hi ,

I have the following

{"Col": [ { "Text": "select1", "Val": "select1" }, { "Text": "lastname", "Val": "lastname" }]}

Its is a JArray.

How can I traverse to extract the value and concatenate with a semicolon ?

select1;lastname

 

All replies (2)

Monday, November 11, 2019 6:41 AM ✅Answered

All sorted using linq


Monday, November 11, 2019 6:54 AM

Hi robby32,

How can I traverse to extract the value and concatenate with a semicolon ?

select1;lastname

You can use the JsonConvert.DeserializeObject Method to deserialize the JArray and loop the object to get the string you expected.

Please refer to below code:

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; }
        }

Best Regard,

Yang Shen