1

I have an JSON Object like this:

    public class RootObject
{
    public string Number { get; set; }
    public string ChannelType { get; set; }
    public string ServiceName { get; set; }
    public List<ImageInfos> ImageInfos { get; set; }
    public bool IsInMixedFolder { get; set; }
    public string Name { get; set; }
    public string Id { get; set; }
    public string DateCreated { get; set; }
    public string DateModified { get; set; }
    public string DateLastSaved { get; set; }
    public List<object> LockedFields { get; set; }
    public string ParentId { get; set; }
    public List<string> Studios { get; set; }
    public List<string> Genres { get; set; }
    public ProviderIds ProviderIds { get; set; }
}

With ImageInfos like this:

    public class ImageInfos
{
    private string Path;
    private string Type;
    private string DateModified;

    public ImageInfos(string Path, string Type, string DateModified)
    {
        this.Path = Path;
        this.Type = Type;
        this.DateModified = DateModified;
    }

I am trying to fill the json ImageInfos-List with values and serialize it to a new string:

            jsonObject = JsonConvert.DeserializeObject<RootObject>(json);

            List<ImageInfos> newInfos = new List<ImageInfos>();
            newInfos.Add(new ImageInfos("Test", "Test", "Test"));
            jsonObject.ImageInfos = newInfos;
            string newJSON = JsonConvert.SerializeObject(jsonObject);

But the new json looks like that:

"{\"Number\":\"35\",\"ChannelType\":\"TV\",\"ServiceName\":\"DVBLink\",\"ImageInfos\":[{}],\"IsInMixedFolder\":false,\"Name\":\"13th Street HD\",\"Id\":\"466ab1fb9be4f77e65ad2b229a15c326\",\"DateCreated\":\"2015-09-14T12:55:51.4081476Z\",\"DateModified\":\"0001-01-01T00:00:00.0000000Z\",\"DateLastSaved\":\"2015-10-17T13:34:37.4214116Z\",\"LockedFields\":[],\"ParentId\":\"00000000000000000000000000000000\",\"Studios\":[],\"Genres\":[],\"ProviderIds\":{\"ProviderExternalId\":\"13250000\"}}"

Why is ImageInfos empty in the new JSON string ?

1
  • All the properties of ImageInfos are private. Json.NET will not serialize them by default. To force it to serialize private properties, add the [JsonProperty] attribute to each. Commented Oct 21, 2015 at 8:18

1 Answer 1

2

All the properties of ImageInfos are private. Json.NET will not serialize them by default, so the resulting JSON shows an empty object.

To force Json.NET to serialize private properties, add the [JsonProperty] attribute to each:

public class ImageInfos
{
    [JsonProperty]
    private string Path;
    [JsonProperty]
    private string Type;
    [JsonProperty]
    private string DateModified;

    public ImageInfos(string Path, string Type, string DateModified)
    {
        this.Path = Path;
        this.Type = Type;
        this.DateModified = DateModified;
    }
}

(Or, make them public).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.