简体   繁体   中英

create json structure for in wcf(dynamic key for json)

How to declare dynamic key using class?

{
"balls": {"a8bf081d-eaef-44db-ba25-97ed8c0b30ef": {"team": {"runs": 1,
          "extras": 0,
          "ball_count": 1,
          "wicket": 0
        }
      },
}}

Based on your last Json format, still not valid but close. You have the following class

public class Team
{
    public int runs { get; set; }
    public int extras { get; set; }
    public int ball_count { get; set; }
    public int wicket { get; set; }
}

Using a Dictionary<string, Team> you can simply:

var objFoo = new
{
    balls = new Dictionary<string, Team>
    {
        {
            "a8bf081d-eaef-44db-ba25-97ed8c0b30ef",
            new Team{
                runs=1,
                extras=0,
                ball_count=1,
                wicket=0
            }
        }
    }
};

var result = JsonConvert.SerializeObject(objFoo);

online Exemple

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM