0

Rebuilding a java app for android I built in Windows Visual Studio. Need help on using JSON strings in Visual Studio Visual C# Forms app (.NET Framework).

I'm creating a new file format to be able to transfer data to different robots in my company. I used a list map for my android app and the file contains a JSON string. Is it possible to add the string into a list on Visual C# Forms (.NET Framework) for view in a list box? Sample is provided.

[{"VALUE":"03","ATTRIBUTE":"Laayelbxw"},
 {"VALUE":"01","ATTRIBUTE":"Leruaret"},
 {"VALUE":"08","ATTRIBUTE":"Lscwbryeiyabwaa"},
 {"VALUE":"09","ATTRIBUTE":"Leruxyklrwbwaa"}]
  • 1
    Yes you can have. – Aria Feb 12 '19 at 14:28
  • 4
    Possible duplicate of [Deserialize JSON with C#](https://stackoverflow.com/questions/7895105/deserialize-json-with-c-sharp) – Maksym Labutin Feb 12 '19 at 14:29
  • Adding an important note: make sure all the object's attributes are instanciated because JsonConvert.DeserializeObject() will fail if any of them are not set to a reference. – Mahdi Benaoun Feb 12 '19 at 15:29
  • @MahdiBenaoun what do you mean by *"all the object's attributes are instanciated"* ? Do you mean all the json members are set? – Cid Feb 12 '19 at 16:46

2 Answers2

5

Of course you can !

The simpliest way I know to deserialize a JSON in C# is using the Newtonsoft Json nuget package.

In example :

/*
 * This class represent a single item of your collection.
 * It has the same properties name than your JSON string members
 * You can use differents properties names, but you'll have to use attributes
 */
class MyClass
{
    public int VALUE { get; set; }
    public string ATTRIBUTE { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        var myJSON = "[{\"VALUE\":\"03\",\"ATTRIBUTE\":\"Laayelbxw\"},{\"VALUE\":\"01\",\"ATTRIBUTE\":\"Leruaret\"},{\"VALUE\":\"08\",\"ATTRIBUTE\":\"Lscwbryeiyabwaa\"},{\"VALUE\":\"09\",\"ATTRIBUTE\":\"Leruxyklrwbwaa\"}]";

        //                 V---------V----- Namespace is Newtonsoft.Json
        var MyCollection = JsonConvert.DeserializeObject<List<MyClass>>(myJSON);
        // Tadaam ! You now have a collection of MyClass objects created from that json string

        foreach (var item in MyCollection)
        {
            Console.WriteLine("Value : " + item.VALUE);
            Console.WriteLine("Attribute : " + item.ATTRIBUTE);
        }
        Console.Read();
    }
}

Output

Value : 3
Attribute : Laayelbxw
Value : 1
Attribute : Leruaret
Value : 8
Attribute : Lscwbryeiyabwaa
Value : 9
Attribute : Leruxyklrwbwaa
Cid
  • 14,968
  • 4
  • 30
  • 45
3

It's gonna be something like this.

public class JsonExample
{
    public int VALUE { get; set; }

    public string ATTRIBUTE { get; set; }
}

public void GetJson()
{
    string json = "your string";
    var xpto = JsonConvert.DeserializeObject<List<JsonExample>>(json);
}
  • Your answer is also good, when you was trying to post it, @Cid post his own faster than you. – Aria Feb 12 '19 at 14:38
  • Yup, I've been a bit faster, but you still deserve an upvote since your answer is accurate – Cid Feb 12 '19 at 14:39