0

I have the following json string:

{
    "var1": "admin", // username
    "var2":"2021-06-22T06:16:31.838Z", // timestamp
   
        
    "var3": [
        {
          "name": "NAME_ARABIC",
          "stringValue": "اسامہ محمد اسامہ عالم عالم"
        },
        {
          "name": "NAME_ENGLISH",
          "stringValue": "Alam Alam"
        }
      ],
    "var4": [
        {
          "code": "MOFA",
          "fullName": "Ministry of Foreign Affairs",
          "shortName": "MOFA",
          "typeOrganization": "Firm",
          "fullNameOrganization": "Ministry of Foreign Affairs",
          "shortNameOrganization": "MOFA",
          "codeOrganization": "MOFA"
        }
      ]
}

I am using the following code to get the values from the json and I can get var1 and var2 but I am not sure how to get var3 and var4 that are arrays. Can you please help me with that?

        [HttpPost]
        [Route("addCardRequest")]
        public ContentResult addCardRequest([FromBody] Object json)
        {
            string jsonInput = json.ToString();

            Dictionary<string, string> jsonDic = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonInput);

            hmacRequest hmacReq = new hmacRequest();
            hmacReq.var1 = jsonDic["var1"];
            hmacReq.var2 = jsonDic["var2"];
        }
4
  • 2
    Is there a reason you don't serialize to a specific object instead of the "generic" approach of a dictionary? eg. use past as json classes feature of visual studio learn.microsoft.com/en-us/visualstudio/ide/reference/… Commented May 13, 2024 at 12:29
  • 1
    It's not a dictionary in JSON. It's an array of objects. But you could deserialize to a model and then use e.g. LINQ to get a Dictionary. If you have control over the source and having a Dictionary was the initial plan, have them serialize to something like "..., name": { "ARABIC" : "...", "ENGLISH": "Alam Alam"}, ..." Commented May 13, 2024 at 12:32
  • 1
    Is var1 always a string? Is var3 always an array of name/stringValue pairs? etc. If so, then use a proper class structure that matches that format. Commented May 13, 2024 at 12:48
  • @RandRandom the object is very big I just shared a small part so I was thinking if there's a generic way otherwise, I will serialize it to the specific object. Commented May 14, 2024 at 5:35

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.