user2155362
user2155362

Reputation: 1703

How to express array type in c# object

In my project, client send a json string back to server side. Below is parts of the json string.

"xy":[170,226],"cell":[["catalog_2","catalog_2"],["input_423","423"],["input_421","421"]]

On server side, I try to parse the json string into custom object. But I don't know which type can express the cell and xy property?

Please help, thanks.

Ps : I use JavaScriptSerializer to parse the json string.

Upvotes: 2

Views: 89

Answers (2)

mason
mason

Reputation: 32702

public class RootObject
{
    public List<int> xy { get; set; }
    public List<List<string>> cell { get; set; }
}

It's a simply a list of ints, and then a list of list of strings. I don't think that's what you want, but that's unfortunately what you get due to how the JSON is structured. Perhaps "xy" should be a nested object and not an array? Do you have any control over the JSON?

By the way, I generated that object with json2csharp.

Upvotes: 0

Trevor Pilley
Trevor Pilley

Reputation: 16413

The yx property contains an array of int so you can use either int[] or List<int> the cell property is an array of arrays which are strings. Using the JSON to C# converter yields the following class:

public class RootObject
{
    public List<int> xy { get; set; }
    public List<List<string>> cell { get; set; }
}

Upvotes: 1

Related Questions