C
C#3y ago
reeeeeee

Get multiple objects in JObject path

I have this kind of XML JOBject
"cust_toLegalEntity": {
"FOEntity": {
"externalCode": "202",
"name": "Test1",
"startDate": "2022-01-01T00:00:00"
},
"FOEntity": {
"externalCode": "204",
"name": "Test2",
"startDate": "2022-01-01T00:00:00"
}
}
"cust_toLegalEntity": {
"FOEntity": {
"externalCode": "202",
"name": "Test1",
"startDate": "2022-01-01T00:00:00"
},
"FOEntity": {
"externalCode": "204",
"name": "Test2",
"startDate": "2022-01-01T00:00:00"
}
}
I would like to retrieve a list of external Codes. I tried with jobj.SelectToken("cust_toLegalEntity.FOEntity.externalCode) or with jobj.SelectTokens("cust_toLegalEntity.FOEntity.externalCode)m which works only if there is only 1 FOEntity in the list. If there are more than 1, it returns null. Any idea how should I specify the path?
7 Replies
FroH.LVT
FroH.LVT3y ago
what do u mean XML JObject ? Isn't it a json ?
reeeeeee
reeeeeeeOP3y ago
Yeah sorry for my bad working.. its a GET request which returns XML response, which is parsed to json.
FroH.LVT
FroH.LVT3y ago
are u using Newtonsoft.Json.Linq?
reeeeeee
reeeeeeeOP3y ago
yes
FroH.LVT
FroH.LVT3y ago
I suggest you parse cust_toLegalEntity to jArray then you can loop it
reeeeeee
reeeeeeeOP3y ago
This might work yes, ty
sunder.cc
sunder.cc3y ago
the json youve send was a bit malformed but a JToken is an ienumerable and you can simply .ToList() it
var token= JObject.Parse(json)["cust_toLegalEntity"];
var tokenList= token.ToList();
var token= JObject.Parse(json)["cust_toLegalEntity"];
var tokenList= token.ToList();
Want results from more Discord servers?
Add your server