Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

277
Vistas
system.text.json search a dynamic json

I have this JSON string:

string jsonString =
  "{
    "users":[
      {"name":"John", "code":"white", "job":"actor"},
      {"name":"Oliver", "code":"black", "job": "seller"}
     ]
  }"

then deserialize it using:

JsonElement je = JsonDocument.Parse(jsonString).RootElement;
JsonElement.ArrayEnumerator users = je.GetProperty("users").EnumerateArray();

with system.text.json how can I get first JsonElement whose code is "black"? I mean without loops (foreach, ...).

with newtonsoft.json I could simply do this:

dynamic json = JsonConvert.DeserializeObject(jsonString);
dynamic user = json.SelectToken("[?(@.code == 'black')]");
string name = user["name"], job = user["job"];
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

how can I get first JsonElement whose code is "black"

I would create classes that represent your json structure and then deserialize to those classes.

You can create your classes as such:

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; }
}

Then to deserialize:

string jsonString = "{\"users\":[{\"name\":\"John\",\"code\":\"white\",\"job\":\"actor\"},{\"name\":\"Oliver\",\"code\":\"black\",\"job\":\"seller\"}]}";
People people = JsonSerializer.Deserialize<People>(jsonString);

Now you can get the first User object where code is black:

 var usr = people.Users.Where(u => u.Code == "black").FirstOrDefault();

Here's the example you can run.

UPDATE PER COMMENT

it is dynamic JSON

You can deserialize your json to JsonElement and then try and get the properties you want.

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));
}

*You can test this code there

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda