0

please look at this json sample

I want to make a model for this json in asp.net mvc:

 {
    "MatchStat":{
        {
            "Corner":[10,3], 
            "Offside":[2,1], 
        }
    },

    "FirstTeamArrange":{ 
        "Tactic":"4-4-2",
        "PlayerPos":[0,["PlayerID"],["PlayerID"],["PlayerID"]]
    }
    "SecondTeamArrange":{ 
        "Tactic":"4-4-2",
        "PlayerPos":[0,["PlayerID"],["PlayerID"],["PlayerID"]]
    }

}

what is [0,["PlayerID"],["PlayerID"],["PlayerID"]]?

is it a type of array?

how can I write property for it?

this is what I tried:

  public class MatchModel
{
    public TeamArrange FirstTeamArrange { get; set; }
    public TeamArrange SecondTeamArrange { get; set; }
    public MatchStat Stat { get; set; }

    public MatchModel()
    {

    }


    public class TeamArrange
    {
        public String Tactic { get; set; }

        public string[,,] PlayerPos { get; set; }????????????????????????????

        public TeamArrange()
        {
            Tactic = "";
            PlayerPos=???????????????????????????

        }

    }
    public class MatchStat
    {
        public int[] Corner { get; set; }
        public int[] Offside { get; set; }

        public MatchStat()
        {
            Corner=new int[2];
            Offside=new int[2];
        }
    }


}

could your please correct my mistake?

what I need to write instead of question marks?

  • You may want to "Edit" / "Paste special" your JSON as classes in Visual Studio: https://stackoverflow.com/questions/18526659/how-to-show-the-paste-json-class-in-visual-studio-2012-when-clicking-on-paste – Richardissimo Dec 15 '18 at 10:26

1 Answers1

0

You need a list of JToken to deserialize your PlayerPos key data.

Use below class structure to deserialize your json correctly.

class Root
{
    public Dictionary<string, List<int>> MatchStat { get; set; }
    public TeamArrange FirstTeamArrange { get; set; }
    public TeamArrange SecondTeamArrange { get; set; }
}

class TeamArrange
{
    public string Tactic { get; set; }
    public List<JToken> PlayerPos { get; set; }
}

Usage:

Root root = JsonConvert.DeserializeObject<Root>(json);

foreach (var item in root.FirstTeamArrange.PlayerPos) //foreach (var item in root.SecondTeamArrange.PlayerPos)
{
    if (item.Type == JTokenType.Integer)
        Console.WriteLine(item.Value<int>());
    else
        if (item.Type == JTokenType.Array)
        {
            var arr = item.ToObject<string[]>();

            foreach (var innerItem in arr)
                Console.WriteLine(innerItem);
        }
}

Console.ReadLine();

Output:

enter image description here

er-sho
  • 9,581
  • 2
  • 13
  • 26
  • what is this? [0,["PlayerID"],["PlayerID"],["PlayerID"]] in c#? is it an array? what does mean 0 here? – lili farabari Dec 15 '18 at 11:12
  • this is an array of `0` and array of array string `PlayerID`. i think `0` its something like index in your json...If this json is from 3rd party vendor then you should contact those people and let me know – er-sho Dec 15 '18 at 11:14
  • no it's not from 3rd party, it's from our higher developer and I dont access to him now! it's a sample json and I need make model for it! – lili farabari Dec 15 '18 at 11:19
  • I already gave you model and deserialization code you can use it and if my answer was helpful to you then you can mark tick on left side of answer to make it green :) – er-sho Dec 15 '18 at 11:50
  • thanks shoaib, of course your answer and comments are useful, but I have to not use jtoken, I only need to know what is PlayerPos, an jagged array, simple array,multi dimentional array,.... and write a property for it. thanks again, I found I need to ask it from the same developer who wrote it. – lili farabari Dec 15 '18 at 12:35
  • your `PlayerPos` is an `Array [ of 0 and Array of [ strings ] ]`. if you dont want to use JToken then you can use it like `public List PlayerPos { get; set; }` – er-sho Dec 15 '18 at 12:56