0

Below is my JSON information where you can notice that there is not any key defined.

{"KOA": {"test.jpg": "xyz.com/images/test.jpg"}}

I am Looking the Output like:

string _imgName=  ms-koa-acazia-ceramic.jpg
string _imgUrl= xyz.com/images/test.jpg

"KOA" is also a dynamic value it can be changed so I can not fix it.

Amar Yadav
  • 17
  • 5
  • What do you mean by '"KOA"` is a dynamic value? Do you mean `KOA` will not always be referred to by that name or that the underlying properties will change? – Patrick Mcvay Nov 10 '20 at 17:53
  • KOA will not always be referred to by that name... – Amar Yadav Nov 10 '20 at 17:58
  • You should really look into how `JSON` is formatted a little more. Your current `JSON` is not even `JSON` really. https://www.w3schools.com/js/js_json_intro.asp – Patrick Mcvay Nov 10 '20 at 18:02

1 Answers1

1

One approach you can use to achieve this is to use dictionaries to deserialize the JSON object

        const string jsonString = "{\"KOA\": { \"test.jpg\": \"xyz.com / images / test.jpg\"}}";
        var jsonObject = JsonConvert.DeserializeObject<IDictionary<string, IDictionary<string, string>>>(jsonString);

        string firstKey = jsonObject.Keys.First();

        string _imgName = jsonObject[firstKey].Keys.First();
        string _imgUrl = jsonObject[firstKey].Values.First();
  • IN case of multiple pair how can we get the value {"KOA": {"xyz.jpg": "xyz.com/sigaro-ivory-essentials-ceramic-a.jpg", "abc.jpg": "abc/sigaro-ivory-essentials-ceramic-b.jpg"}} – Amar Yadav Nov 12 '20 at 15:31
  • 1
    in this case, I would use the JObjet class first to obtain the value of KOA, and then I would use a dictionary to get the other values – Mario Sandoval Nov 12 '20 at 16:35
  • 1
    const string jsonString = "{\"KOA\": { \"xyz.jpg\": \"xyz.com/sigaro-ivory-essentials-ceramic-a.jpg\", \"abc.jpg\": \"abc/sigaro-ivory-essentials-ceramic-b.jpg\"}}"; var jsonObject = JsonConvert.DeserializeObject(jsonString); var koaName = jsonObject.First.Path; var koaValue = jsonObject[koaName].Value(); var values = koaValue.ToObject>(); – Mario Sandoval Nov 12 '20 at 16:36