0

I am trying to parse a JSON object, as shown below, where the header object would be a random alpha numeric number. Also, sometimes the JSON could contain only one object.

{
  "48ce0e9a-ee27-4e7b-93a5-4f219974f62c": {
    "preview": {
      "FRONT": "https://123avb.jpg",
      "INSIDE_TOP": "https://456ihn.jpg",
      "INSIDE_BOTTOM": "https://987ung.jpg",
      "BACK": "https://ikh980.jpg"
    },
    "print": {
      "primary": "https://iifuer.pdf",
      "secondary": "https://09e491.pdf"
    }
  },
  "d767e1a6-7afe-4d17-85f7-d512a0ca0b14": {
    "preview": {
      "FRONT": "https://0ff7cc7.jpg",
      "BACK": "https://64e7a8.jpg"
    },
    "print": {
      "primary": "https://73fbdc.pdf"
    }
  }
}

I created classes for these objects as:

namespace FulfillmentModel
{
    public class Baseclass
    {
        public G1 G1 { get; set; }

        public Env Env1 { get; set; }
    }

    public class Preview
    {
        [JsonProperty("FRONT")]
        public string FRONT { get; set; }

        [JsonProperty("INSIDE_TOP")]
        public string INSIDE_TOP { get; set; }

        [JsonProperty("INSIDE_BOTTOM")]
        public string INSIDE_BOTTOM { get; set; }

        [JsonProperty("BACK")]
        public string BACK { get; set; }
    }

    public class Print
    {
        [JsonProperty("primary")]
        public string primary { get; set; }
        [JsonProperty("secondary")]
        public string secondary { get; set; }
    }

    public class G1
    {
        public Preview preview { get; set; }
        public Print print { get; set; }
    }

    public class Preview2
    {
        public string FRONT { get; set; }
        public string BACK { get; set; }
    }

    public class Print2
    {
        public string primary { get; set; }
    }

    public class Env
    {
        public Preview2 preview { get; set; }
        public Print2 print { get; set; }
    }
}

I am reading the JSON object from an API. I am using the following code to convert it into a BaseClass:

string Jsontring= response.Content.ReadAsStringAsync().Result;
var verificationResult = JsonConvert.DeserializeObject<Baseclass>(Jsontring);

but the object in the BaseClass is null.

I'm not sure what I am doing wrong. Can you please help?

2 Answers 2

3

In your JSON, there are keys 48ce0e9a-ee27-4e7b-93a5-4f219974f62c and d767e1a6-7afe-4d17-85f7-d512a0ca0b14 in the outermost object. But these keys are not present in the Baseclass model class you are deserializing into, which contains G1 and Env properties. You could mark these properties with [JsonProperty] attributes, but since you said that these keys are random, the better way to handle it would be to deserialize into a Dictionary<string, G1>. (Alternatively you can use Dictionary<Guid, G1> if you are sure that the alphanumeric keys will always represent GUIDs).

var verificationResult = JsonConvert.DeserializeObject<Dictionary<string, G1>>(Jsontring);

Now verificationResult will contain a dictionary, where the keys are the random keys from the JSON. The values corresponding to each key will be a G1 object, which has populated Preview and Print objects.

Demo fiddle here: https://dotnetfiddle.net/kzGihd

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

2 Comments

Dictionary<Guid, G1> would be better.
@PMExtra Perhaps. The OP did not say that the keys were Guids (though they do appear to be); just that they were "random alphanumeric numbers". Given the loosey goosey specification, string keys are safer.
0

Did you try to add await before the async function?

await response.Content.ReadAsStringAsync()

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.