Home > Software engineering >  How to get information from object in C# Visual Studio that used JSON string to object method?
How to get information from object in C# Visual Studio that used JSON string to object method?

Time:10-05

How to get information from object in C# Visual Studio that used JSON string to object method?

string jsonStr = "[{'Name':'John Simith','Age':35},{'Name':'Pablo Perez','Age':'34'}]";

JavaScriptSerializer jsss = new JavaScriptSerializer();
objectstrPDItem = jsss.Deserialize(jsonStr, typeof(object));

How to get information from objectstrPDItem?

CodePudding user response:

try this using Newtonsoft.Json

List<Person> persons = JsonConvert.DeserializeObject<List<Person>>(jsonStr);

class

 public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

test

foreach (var person in persons) Console.WriteLine($"({person.Name},{person.Age})");

output

(John Simith,35)
(Pablo Perez,34)

CodePudding user response:

This works great for me: https://forums.asp.net/t/2099529.aspx?Decoding of json data failing using json net

JArray jsonVal = JArray.Parse(jsonStr) as JArray;
dynamic albumsjsonVal = jsonValDenis;
foreach (dynamic album in albumsjsonVal)
{
    var test = album.id;
    var testname = album.name; 
    //Response.Write(album.id   "  --  "   album.paymentDue   "  <br />");

}

  • Related