Home > Back-end >  How to write correct JSON file in C#?
How to write correct JSON file in C#?

Time:03-30

I need to write the following data into a text file using JSON format in C#. The brackets are important for it to be valid JSON format.

    {
      "id": 1,
      "houseattributes": [
        {
          "example_type": "big",
          "examplevalue": "black"
        },
        {
          "example_type": "small",
          "examplevalue": "white"
        },
        {
          "example_type": "very big",
          "examplevalue": "pink"
        },
       ...........
      ],
      "description": "some description..",
      "image": "image url...",
      "name": "house name...",
      "edition": 1
    }

With this Class i read the input Json but its in a wrong format and i must modify to the correct valid Json... and i must manually(code) adding the description image url to the parsed Json.

    public class Houseattribute
    {
        public string example_type { get; set; }
        public string examplevalue { get; set; }
    }

    public class Root
    {
        public string name { get; set; }
        public int edition { get; set; }
        public List<Houseattribute> houseattributes { get; set; }
    }

how can i get the correct output json?

Input Json:

{
  "houseattributes": [
    {
      "example_type": "big",
      "examplevalue": "black"
    },
    {
      "example_type": "small",
      "examplevalue": "white"
    },
    {
      "example_type": "very big",
      "examplevalue": "pink"
    },
   .........
  ],
  "edition": 1,
  "name": "house name..."
}

CodePudding user response:

Change your model to include the extra properties:

public class Houseattribute
{
    public string example_type { get; set; }
    public string examplevalue { get; set; }
}

public class Root
{
    public int id { get; set; }
    public string name { get; set; }
    public string description { get; set; }
    public string image { get; set; }
    public int edition { get; set; }
    public List<Houseattribute> houseattributes { get; set; }
}

And then deserialize your JSON to the model, fill in the additional properties, and reserialize it. Example with JSON.NET:

Root obj = JsonConvert.DeserializeObject<Root>(inputJson);
obj.id = 5;
obj.description = "Test";
obj.image = "https://www.example.com/images/myimage.jpg";
string outputJson = JsonConvert.SerializeObject(obj);

Note that I'd suggest updating your properties to follow the naming conventions used by Microsoft (TitleCase). You can decorate the properties with [JsonPropery("name")] (attributes with the same name exist in JSON.NET and System.Text.Json) to ensure they serialize correctly:

public class HouseAttribute
{
    [JsonProperty("example_type")]
    public string ExampleType { get; set; }
(etc.)

CodePudding user response:

just add the additonal fields to the class

public class Root
{
    public string name { get; set; }
    public int edition { get; set; }
    public List<Houseattribute> houseattributes { get; set; }
    public string image {get;set;}
    public string description {get;set;}
}

people are confused by the question becuase you seemed to be able to work out how to add 2 of the fields, but not the missing two

  •  Tags:  
  • c#
  • Related