Since the property name attachment.content
contains .
you can escape it like this:
var tokens = jObj.SelectToken("highlight['attachment.content']");
For more information see:
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I got a json (see below) in a JObject named hits.
I can read single values from this object like this:
string path = hits.SelectToken("_source.path").Value<string>();
But I can't figure out, how to read the value from "highlight.attachment.content". It seems to be a List because "[]".
I tried many ways which are not working.
There is a exception or the result is null
.
Some examples of it I leave below.
How can I get the content of the List in highlight.attachment.content
using SelectTokens
?
{
"_index":"attachments",
"_id":"43",
"_source":{
"path":"C:\\Basis\\\doc\\00000052.pdf",
"attachment":{
"content_type":"application/pdf",
"format":"application/pdf; version=1.3",
}
},
"highlight":{
"attachment.content":[
"Grundstück\nGemarkung : Musterstadt - Testdorf\nFlur : 004\nFlurstück : 18/1\nGröße : 2492 m²\n\nvon Frau Julia <em>Berger</em>"
]
}
}
here some code which is not working:
var a = hits.SelectTokens("highlight.attachment.content").Values<string>();
var a = hits.SelectTokens("highlight.attachment.content").Values<string>();
var a = hits.SelectTokens("highlight.attachment.content[]").Values<string>();
var a = hits.SelectToken("highlight.attachment.content[*]");
var a = hits.SelectTokens("highlight.attachment.content[*]").Values<string>();
var a = hits.SelectTokens("highlight.attachment.content[0]").Values<string>();
var a = hits.SelectTokens("highlight.attachment.content[][0]").Values<string>();
var h = hits.SelectTokens("highlight.attachment.content[*]").Values<List<string>>();
var h = hits.SelectToken("highlight.attachment.content").Value<List<string>>();
var h = hits.SelectToken("highlight.attachment.content[]").Value<List<string>>();
var h = (List<string>)hits.SelectToken("highlight.attachment.content").Value<List<string>>();
var h = (List<string>)hits.SelectToken("highlight.attachment.content[]").Value<List<string>>();
var h = hits.SelectTokens("highlight.attachment.content[*][]").Values<List<string>>();
JArray array = (JArray)hits["highlight.attachment.content"];
var high = hits.SelectToken("highlight.attachment.content");
var first = (JObject)hits["highlight.attachment"]["content"][0];
var highlight = (JObject)hits["highlight.attachment"];
var first = (JObject)hits["highlight.attachment.content[]"];
var first = (JObject)hits["highlight.attachment.content[*]"][0];
Since the property name attachment.content
contains .
you can escape it like this:
var tokens = jObj.SelectToken("highlight['attachment.content']");
For more information see:
Using .NET Core reading the file. Full source
using System.Text.Json.Serialization;
namespace JObjectQuestion.Classes
{
public class RootData
{
public string _index { get; set; }
public string _id { get; set; }
public _Source _source { get; set; }
public Highlight highlight { get; set; }
}
public class _Source
{
public string path { get; set; }
public Attachment attachment { get; set; }
}
public class Attachment
{
public string content_type { get; set; }
public string format { get; set; }
}
public class Highlight
{
[JsonPropertyName("attachment.content")]
public string[] attachmentcontent { get; set; }
}
}
Read the data
var json = File.ReadAllText("data.json");
RootData result = JsonSerializer.Deserialize<RootData>(json);
var content = result.highlight.attachmentcontent;