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"}]
4
  • 1
    Yes you can have. Commented Feb 12, 2019 at 14:28
  • 4
    Possible duplicate of Deserialize JSON with C# Commented Feb 12, 2019 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. Commented Feb 12, 2019 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? Commented Feb 12, 2019 at 16:46

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for a best answer than mine, it is well explained ^^
I'm curious to know why I've been downvoted. Any clue someone?
It is interested for me also, hope @downvoter explain if there any problem, it may because of duplication, do you think so ?
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);
}

2 Comments

Your answer is also good, when you was trying to post it, @Cid post his own faster than you.
Yup, I've been a bit faster, but you still deserve an upvote since your answer is accurate

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.