0

I would like to deserialize a JSON object like this:

[{"Response":"OK","UUID":"89172"},{"Response":"OK","UUID":"10304"}]

into a custom class where it has variables storing Response and UUID. However I would want to deserialize multiple data response such as above example. It will be great if I can use the method ForEach such that I can pop the data out accordingly. Can anyone advise? Many Thanks!

4 Answers 4

3

write this class

public class MyClass
{
   public string Response { get; set; }
   public string UUID { get; set; }
}

then you can deserialize it using the library newtonsoft.json

string jsonString = "[{"Response":"OK","UUID":"89172"},{"Response":"OK","UUID":"10304"}]";
...
...
var myListOfItems= JsonConvert.DeserializeObject<List<MyClass>>(jsonString);

foreach(var item in myListOfItems)
{
   ....
}

FULL CODE IN CONSOLE APPLICATION

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        string jsonString = "[{'Response':'OK','UUID':'89172'},{'Response':'OK','UUID':'10304'}]";

        var items= JsonConvert.DeserializeObject<List<MyClass>>(jsonString);

        foreach (var item in items)
        {
            Console.WriteLine("UUUID: "+item.UUID);
            Console.WriteLine("Response: " + item.Response);
            Console.WriteLine();
        }

        Console.ReadKey();
    }
}

public class MyClass
 {
    public string Response { get; set; }
    public string UUID { get; set; }
 }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Wouldn't .DeserializeObject<MyClass> return a single object ?
Yes, it would. By the way, current (edited) solution won't work as well, because now Newton is deserializing list of Root objects, that by themself contain list of MyClass. So result type is List<Root>, not List<MyClass>.
thanks @ionoy :) i have edited the code, now things should work.
Thank you for this suggestion. Moreover, can I access those items with a key like a dictionary?
can u elaborate the 'key'?
|
0

I would use Json.Net for that. Have a look at Json.Net help in the "Serializing and Deserializing JSON" section.

There they show you how to deserialize the json-string into an object.

Comments

0

You will need Newtonsoft.Json library for this to work:

public class A
{
    public string Response { get; set; }
    public string UUID { get; set; }
}

static void Main(string[] args)
{
    var json = "[{\"Response\":\"OK\",\"UUID\":\"89172\"}, \"Response\":\"OK\",\"UUID\":\"10304\"}]";
    var result = JsonConvert.DeserializeObject<IEnumerable<A>>(json);
    foreach (var a in result)
        Console.WriteLine("Response: {0} UUID: {1}", a.Response, a.UUID);
    Console.ReadKey();
}

Comments

0

I've finally resolved this problem thanks with the help of @Newton Sheikh. Thank you first of all.

First I created a class (Student)

public class Student
{
   public string Response { get; set; }
   public string UUID { get; set; }
}

Then I imported the JSON.NET and created a function:

public List<Student> ReturnAllStudentsList()
    {
        string jsonString = "[{'Response':'OK','UUID':'89172'},{'Response':'OK','UUID':'10304'}]";

        List<Student> Students = new List<Student>(); //Creates a list of custom Type: Student
        var result = JsonConvert.DeserializeObject<List<Student>>(jsonString);
        foreach (var student in result)
        {
            Students.Add(student);
        }
        return Students;
    }

From this point, I have a list of Students. Then in my main program, I call this function:

    private void button1_Click(object sender, EventArgs e)
    {
        List<Student> Students = ReturnAllStudentsList(); // Gets the list from JSON.
        foreach(Student student in Students)
        {
            // Here I can access to each student for every loop cycle.
            MessageBox.Show(student.Response);
        }
    }

Thank you @Newton Sheikh and others help! I hope this example code can help others too! Cheers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.