system.text.json 搜索动态 json
system.text.json search a dynamic json
我有这个 JSON 字符串:
string jsonString =
"{
"users":[
{"name":"John", "code":"white", "job":"actor"},
{"name":"Oliver", "code":"black", "job": "seller"}
]
}"
然后反序列化:
JsonElement je = JsonDocument.Parse(jsonString).RootElement;
JsonElement.ArrayEnumerator users = je.GetProperty("users").EnumerateArray();
with system.text.json 我怎样才能得到代码为“黑色”的第一个 JsonElement?
我的意思是没有循环(foreach,...)。
with newtonsoft.json 我可以简单地这样做:
dynamic json = JsonConvert.DeserializeObject(jsonString);
dynamic user = json.SelectToken("[?(@.code == 'black')]");
string name = user["name"], job = user["job"];
how can I get first JsonElement whose code is "black"
我将创建代表您的 json
结构的 类,然后反序列化为 类.
您可以这样创建 类:
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
public class People
{
[JsonPropertyName("users")]
public List<User> Users { get; set; }
}
public class User
{
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("code")]
public string Code { get; set; }
[JsonPropertyName("job")]
public string Job { get; set; }
}
然后反序列化:
string jsonString = "{\"users\":[{\"name\":\"John\",\"code\":\"white\",\"job\":\"actor\"},{\"name\":\"Oliver\",\"code\":\"black\",\"job\":\"seller\"}]}";
People people = JsonSerializer.Deserialize<People>(jsonString);
现在您可以获得第一个 User
对象,其中 code
是黑色的:
var usr = people.Users.Where(u => u.Code == "black").FirstOrDefault();
Here's the 例如你可以 运行.
根据评论更新
it is dynamic JSON
您可以将 json
反序列化为 JsonElement
,然后尝试获取您想要的属性。
var people = JsonSerializer.Deserialize<JsonElement>(jsonString);
if(people is JsonElement je && je.TryGetProperty("users", out je)){
// obj will be JsonElement if match is found
var obj = je.EnumerateArray().Where(je => je.TryGetProperty("code", out je) && je.GetString() == "black").FirstOrDefault();
// Get what you need
var name = obj.TryGetProperty("name", out JsonElement eleName) ? eleName.GetString() : "No name";
var job = obj.TryGetProperty("job", out JsonElement eleJob) ? eleJob.GetString() : "No job";
Console.WriteLine(string.Format("{0}\n{1}", name, job));
}
*您可以在此处test this编码
我有这个 JSON 字符串:
string jsonString =
"{
"users":[
{"name":"John", "code":"white", "job":"actor"},
{"name":"Oliver", "code":"black", "job": "seller"}
]
}"
然后反序列化:
JsonElement je = JsonDocument.Parse(jsonString).RootElement;
JsonElement.ArrayEnumerator users = je.GetProperty("users").EnumerateArray();
with system.text.json 我怎样才能得到代码为“黑色”的第一个 JsonElement? 我的意思是没有循环(foreach,...)。
with newtonsoft.json 我可以简单地这样做:
dynamic json = JsonConvert.DeserializeObject(jsonString);
dynamic user = json.SelectToken("[?(@.code == 'black')]");
string name = user["name"], job = user["job"];
how can I get first JsonElement whose code is "black"
我将创建代表您的 json
结构的 类,然后反序列化为 类.
您可以这样创建 类:
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
public class People
{
[JsonPropertyName("users")]
public List<User> Users { get; set; }
}
public class User
{
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("code")]
public string Code { get; set; }
[JsonPropertyName("job")]
public string Job { get; set; }
}
然后反序列化:
string jsonString = "{\"users\":[{\"name\":\"John\",\"code\":\"white\",\"job\":\"actor\"},{\"name\":\"Oliver\",\"code\":\"black\",\"job\":\"seller\"}]}";
People people = JsonSerializer.Deserialize<People>(jsonString);
现在您可以获得第一个 User
对象,其中 code
是黑色的:
var usr = people.Users.Where(u => u.Code == "black").FirstOrDefault();
Here's the 例如你可以 运行.
根据评论更新
it is dynamic JSON
您可以将 json
反序列化为 JsonElement
,然后尝试获取您想要的属性。
var people = JsonSerializer.Deserialize<JsonElement>(jsonString);
if(people is JsonElement je && je.TryGetProperty("users", out je)){
// obj will be JsonElement if match is found
var obj = je.EnumerateArray().Where(je => je.TryGetProperty("code", out je) && je.GetString() == "black").FirstOrDefault();
// Get what you need
var name = obj.TryGetProperty("name", out JsonElement eleName) ? eleName.GetString() : "No name";
var job = obj.TryGetProperty("job", out JsonElement eleJob) ? eleJob.GetString() : "No job";
Console.WriteLine(string.Format("{0}\n{1}", name, job));
}
*您可以在此处test this编码