Reputation: 4310
I've got a JObject
(Newtonsoft.json) and I'm extracting some values from it.
I'm using the GetValue
function for example:
item.GetValue("text.English.Status")
but I don't know which language it will be.
So I would like to do something like a regex's match:
text.*.Status
How can I accomplish this?
I tried something like this:
item.First<JToken>(p => p.ToString().Contains("Status"))
And indeed it gives me the current token, but I can't find a way to extract the value without knowing its exact location.
Example json:
{
"string.Language": "Turkish",
"text.English.FullName": "Aşk-ı Beşiktaş ❤",
"text.English.ScreenName": "kardelensimsek9",
"text.English.Status": "19.03 KARA TOPRAĞA BEYAZ KEFENLE GÖMÜLÜRKEN BAŞ UCUMUZA KONCAK SİYAH BEYAZ ATKININ HAYALİYLE YAŞIYORUZ. VAR MI ÖTESİ? ⬛⬜❤"
}
Thanks
Upvotes: 2
Views: 5663
Reputation: 129777
You're close. What you need to do is find the JProperty
in the JObject
whose Name
contains your target string, then get the value from that property.
Here is an example:
string json = @"
{
""string.Language"": ""Turkish"",
""text.English.FullName"": ""Aşk-ı Beşiktaş ❤"",
""text.English.ScreenName"": ""kardelensimsek9"",
""text.English.Status"": ""19.03 KARA TOPRAĞA BEYAZ KEFENLE GÖMÜLÜRKEN BAŞ UCUMUZA KONCAK SİYAH BEYAZ ATKININ HAYALİYLE YAŞIYORUZ. VAR MI ÖTESİ? ⬛⬜❤""
}";
JObject item = JObject.Parse(json);
JProperty prop = item.Properties().FirstOrDefault(p => p.Name.Contains(".Status"));
string value = prop != null ? prop.Value.ToString() : "(value not found)";
Console.WriteLine(value);
Fiddle: https://dotnetfiddle.net/okGdx9
If you want to use Regex, you can replace p.Name.Contains(".Status")
with Regex.IsMatch(p.Name, @"text\..*\.Status")
in the above code.
Upvotes: 3