0

I am new to C# and have investigated without success. I come from programming in PHP and I have the following JSON:

{"data": [
    {  "iD":0 , "name":"woody", "a":4.0, "b": 3.5, "foo": [5,8] },
    {  "iD":1 , "name":"donald", "a":5.0, "b": 2.4, "foo": [4, 2] }
]}

What I need is to create something similar in C#, if possible in a separate class to be able to access the data in the correct way. I tried to create a different array for each item but I think it is not correct.

From what I've researched, based on the JSON I've presented as an example, I have the following data types: "string, int and double". In PHP you can mix this type of data in the same array and call it through indexes but I don't know exactly how to do the same in C #.

Eye, my question is not how to read the JSON that I have put above, but how to create an similar array (or an object) with that data structure and that I can access it from another class.

I hope I was clear, thanks in advance.

2
  • You should create a class with three properties in it. The key-feature of a staically-typed language such as C# is, that you allways know the exact types you get when accessing a property. Thus when you write dataObject.name you get a string and can perform any string-operation, whereas if you would apply any math on that string you´d get a compiler-error. You should definitly read about the basic concepts of C#. Commented Apr 30, 2020 at 7:57
  • Thanks for your reply. I don't have much experience in OOP. Could you give me an example of a class with that structure? I know another option is to create a JSON file and read it from C#. But for a learning topic I would like to know how to do it directly in C#. Commented Apr 30, 2020 at 8:02

1 Answer 1

2

You should create classes/structs to represent this data's structure:

public class Datum {
    public int ID { get; set; }
    public string Name { get; set; }
    public double A { get; set; }
    public double B { get; set; }
    public List<int> C { get; set; }
}

You can either store this Datum directly in a List<Datum>, or like you have done in the JSON, store it in another class as a property named Data:

public class RootObject {
    public List<Datum> Data { get; set; }
}

Then, you can create these objects using object and collection initialisers:

var rootObject = new RootObject {
    Data = new List<Datum> {
        new Datum {
            ID = 0,
            Name = "Woody",
            A = 4.0,
            B = 3.5,
            C = new List<int> { 5, 8 }
        },
        new Datum {
            ...
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I did the appropriate tests and it worked as expected, thanks for the info.

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.