-2

I want to generate this nested json via c# objects

{
  "contest": {
    "name": "eatfast"
  },
  "contestants": {
    "player": [
      {
        "id": 1,
        "name": "KILL",
        "stats": {
          "time": 5
        }
      },
      {
        "id": 2,
        "name": "BILL",
        "stats": {
          "time": 16
        }
      }
    ]
  }
}

Heres what I have

private static string FormatJson(string json)
        {
            dynamic parsedJson = JsonConvert.DeserializeObject(json);
            return JsonConvert.SerializeObject(parsedJson, Formatting.Indented);
        }

private void button2_Click(object sender, EventArgs e)
{
    string json = "{\"contest\": { \"name\": \"eatfast\"},\"contestants\":  {\"player\": [";

    Contestants test = new Contestants
    {
        id = 3,
        name = "JESUS"
        //stats;
    };

    json = json + JsonConvert.SerializeObject(test, Formatting.Indented) + "]}}" ;
    System.IO.File.WriteAllText("test.txt", FormatJson(json));
}

Output

{
  "contest": {
    "name": "eatfast"
  },
  "contestants": {
    "player": [
      {
        "id": 3,
        "name": "JESUS"
      }
    ]
  }
}

Could you give me any ideas on how to add a player in this example, or how to add stats time, appreciate any help

Or should I do it manually using string manipulation?

mleo
  • 11
  • 2
  • you could try this tool: [json2csharp.com](http://json2csharp.com/) and paste your json there. – Felix D. Sep 04 '18 at 06:03
  • Possible duplicate of [How do I turn a C# object into a JSON string in .NET?](https://stackoverflow.com/questions/6201529/how-do-i-turn-a-c-sharp-object-into-a-json-string-in-net) – Felix D. Sep 04 '18 at 06:16
  • 1
    Possible duplicate of [How to auto-generate a C# class file from a JSON object string](https://stackoverflow.com/questions/21611674/how-to-auto-generate-a-c-sharp-class-file-from-a-json-object-string) – Drag and Drop Sep 04 '18 at 06:26
  • or https://stackoverflow.com/questions/36983311/is-there-a-vs-plugin-for-convert-a-json-string-to-an-innerclassentity-class-ju – Drag and Drop Sep 04 '18 at 06:29
  • Possible duplicate of [How to create JSON string in C#](https://stackoverflow.com/questions/1056121/how-to-create-json-string-in-c-sharp) – SᴇM Sep 04 '18 at 06:29
  • or https://stackoverflow.com/questions/34043384/easiest-way-to-parse-json-response – Drag and Drop Sep 04 '18 at 06:29
  • I guess OP wants to serialize object to json, not json to object. – SᴇM Sep 04 '18 at 06:30
  • @SeM, what If I told you it's the same thing. In most documentation Serializing and Deserializing, are on the same documentation page. In fact the complete code for Serializing and Deserializing, differs by 2 chars. – Drag and Drop Sep 04 '18 at 06:52
  • @DragandDrop _"In same documentation page"_ is not an enough criteria to accept something as "same thing". – SᴇM Sep 04 '18 at 07:01

1 Answers1

0

As per comment this is what your class would look like (json2csharp.com)

public class RootObject
{
    public Contest contest { get; set; }
    public Contestants contestants { get; set; }
}

public class Contest
{
    public string name { get; set; }
}   

public class Player
{
    public int id { get; set; }
    public string name { get; set; }
    public Stats stats { get; set; }
}

public class Stats
{
    public int time { get; set; }
}

public class Contestants
{
    public List<Player> player { get; set; }
}

All got to do then is using Json.NET (available via NuGet):

RootObject obj = JsonConvert.DeserializeObject<RootObject>(yourjsonString);

and

RootObject obj = new RootObject(); //proper initialisation of your object needed
string json = JsonConvert.SerializeObject(obj);
Felix D.
  • 4,811
  • 8
  • 38
  • 72
  • why randomly downvoting everything ?! – Felix D. Sep 04 '18 at 06:09
  • Probably someone who doesn't approve of "give me the code" kind of questions. I'm not sure why those people downvote good answers, though. I guess it's because they feel that the question shouldn't be answered at all. – ProgrammingLlama Sep 04 '18 at 06:09
  • @John yeah I guess that's the case. I still think even if the question is "give me code" ppl and the OP itself can still learn from those answers... – Felix D. Sep 04 '18 at 06:10
  • 1
    Or the fact that 80% of C# json question have exactly this answer and question. – Drag and Drop Sep 04 '18 at 06:14
  • @DragandDrop i am to blame for answering a question that is a duplicate :D ? – Felix D. Sep 04 '18 at 06:16
  • @DragandDrop so why dont u guys instead of downvoting me flag the question as a dupe :D ? – Felix D. Sep 04 '18 at 06:17
  • kinda https://meta.stackexchange.com/questions/10841/how-should-duplicate-questions-be-handled, but im on page 7 of Google can't find an adequate duplicate with both json to csharp and PAste special answer and dynamyc type. – Drag and Drop Sep 04 '18 at 06:17