-4

I am getting Json data with multiple attributes but I want to fetchg only single out of it

[{"Name":"Test","GroupId":"UST","LocationGroupType":"Container","Locale":"en-US","ParentLocationGroup":{"Id":{"Value":4429},,"LgLevel":0,"Users":"0","Admins":"0","Devices":"221","Id":{"Value":5545},"Uuid":"-787d87c8fd3a"}]

I want to get ParentLocationGroup only, how can I do that?

R15
  • 13,982
  • 14
  • 97
  • 173
  • Why don't you use a JSON parser? Have a look at [here](https://stackoverflow.com/a/21678495/2436598). – cumhuryalcin Nov 19 '19 at 11:14
  • I'll just put this link into it, because I'm too lazy to give you an example. [Example of SelectToken](https://www.newtonsoft.com/json/help/html/SelectToken.htm) – Christoph K Nov 19 '19 at 11:15
  • @Steve - Now json fixed. pl check – R15 Nov 19 '19 at 11:17
  • I think your question is not clear. You can change your server to send only the thing you want. You can use graphql server. If you can't modify the server, you can only filter data in client-side – canbax Nov 19 '19 at 11:20

3 Answers3

2

Your json is invalid. No extra comma or missing closing } at the end and you have duplicate key name Id. Your json should be like this.

[ { "Name": "Test", "GroupId": "UST", "LocationGroupType": "Container", "Locale": "en-US", "ParentLocationGroup": { "LgLevel": 0, "Users": "0", "Admins": "0", "Devices": "221", "Id": { "Value": 5545 }, "Uuid": "-787d87c8fd3a" } } ]

And solution...

    public class Id
    {
        public int Value { get; set; }
    }

    public class ParentLocationGroup
    {
        public int LgLevel { get; set; }
        public string Users { get; set; }
        public string Admins { get; set; }
        public string Devices { get; set; }
        public Id Id { get; set; }
        public string Uuid { get; set; }
    }

    public class RootObject
    {
        public string Name { get; set; }
        public string GroupId { get; set; }
        public string LocationGroupType { get; set; }
        public string Locale { get; set; }
        public ParentLocationGroup ParentLocationGroup { get; set; }
    }

 string jsonString = "[{\"Name\":\"Test\",\"GroupId\":\"UST\",\"LocationGroupType\":\"Container\",\"Locale\":\"en-US\",\"ParentLocationGroup\":{\"LgLevel\":0,\"Users\":\"0\",\"Admins\":\"0\",\"Devices\":\"221\",\"Id\":{\"Value\":5545},\"Uuid\":\"-787d87c8fd3a\"}}]";

 List<RootObject> roots = JsonConvert.DeserializeObject<List<RootObject>>(jsonString);

 ParentLocationGroup parent = roots.FirstOrDefault().ParentLocationGroup;
caner
  • 721
  • 5
  • 21
1

You can parse the string to JArray and then parse the first item using JObject.Parse

var jsonArray = JArray.Parse(jsonstring);
JObject obj = JObject.Parse(jsonArray[0].ToString());
Console.WriteLine(obj["ParentLocationGroup"]);
Console.WriteLine(obj["ParentLocationGroup"]["Id"]["Value"]);
R15
  • 13,982
  • 14
  • 97
  • 173
Krishna Varma
  • 4,238
  • 2
  • 10
  • 25
-1

Just use Newtonsoft.Json.

dynamic obj = JsonConvert.DeserializeObject(jsonStr);
Console.Write(obj[0].ParentLocationGroup);

Note your json root is an array, so hence the obj[0] to just get the first array item.