I have tried many different examples of trying to return several values from a JObject, but still am struggling to return a string list of ids. What I would like to do is return only the id values where definition is equal to "Document" ignoring the other ids where name is equal to "Author". I am struggling to even print out all the ids. Below is my json file and what I have so far. Right now all it prints out System.Collections.Generic.List`1[System.String], so not even getting the text value yet but its only collecting 1. I have not even tried adding in the pieces to ignore the Author values.
So my ideal collection would be 45, 46, 47 and ignoring 123, 124, 125. Can anyone point me in the right direction here?
This JSON could even be more complex, where there could be more entities of type "Document" nested inside another Document..
{
"count": 3,
"entities": [{
"id": 45,
"definition": "Document",
"attributes": [{
"name": "Valid",
"value": "False"
}, {
"name": "Released",
"value": "False"
}, {
"name": "Open",
"value": "False"
}, {
"name": "Author",
"entities": [{
"id": 123,
"ref": []
}
]
}
],
"ref": []
}, {
"id": 46,
"definition": "Document",
"attributes": [{
"name": "Valid",
"value": "False"
}, {
"name": "Released",
"value": "False"
}, {
"name": "Open",
"value": "False"
}, {
"name": "Author",
"entities": [{
"id": 124,
"ref": []
}
]
}
],
"ref": []
}, {
"id": 47,
"definition": "Document",
"attributes": [{
"name": "Valid",
"value": "False"
}, {
"name": "Released",
"value": "False"
}, {
"name": "Open",
"value": "False"
}, {
"name": "Author",
"entities": [{
"id": 125,
"ref": []
}
]
}
],
"ref": []
},
],
"skip": 0,
"take": 100
}
This below is my failed attempt at a method
public static List<string> GetAllDocIds(string json)
{
List<string> returnList = new List<string>();
JObject reader = JObject.Parse(json);
IEnumerable<JToken> ids = reader.SelectTokens("$entities.id");
foreach (JToken id in ids)
{
returnList.Add(id.ToString());
}
return returnList;
}
CodePudding user response:
IMHO the easiest way would be to deserialize json using Newtonsoft.Json serializer or a naitive net one.
var json="your json";
var documents = JsonConvert.DeserializeObject<Document>(json);
var ids= documents.Entities.Where(e => e.Definition=="Document").Select(i=>i.Id).ToList();
classes
public partial class Document
{
[JsonProperty("count")]
public long Count { get; set; }
[JsonProperty("entities")]
public DocumentEntity[] Entities { get; set; }
[JsonProperty("skip")]
public long Skip { get; set; }
[JsonProperty("take")]
public long Take { get; set; }
}
public partial class DocumentEntity
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("definition")]
public string Definition { get; set; }
[JsonProperty("attributes")]
public Attribute[] Attributes { get; set; }
[JsonProperty("ref")]
public object[] Ref { get; set; }
}
public partial class Attribute
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("value", NullValueHandling = NullValueHandling.Ignore)]
public string Value { get; set; }
[JsonProperty("entities", NullValueHandling = NullValueHandling.Ignore)]
public AttributeEntity[] Entities { get; set; }
}
public partial class AttributeEntity
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("ref")]
public object[] Ref { get; set; }
}