-3

I have a problem to iterate a object. After Deserialize a json, I have some data like a object[]. But I only need some values.

this is that part of my code:

object[] datos;
StreamReader r = new StreamReader( Server.MapPath( "~/EmpresasLista.json" )   ); // later will come from API
string jsonString = r.ReadToEnd();
JavaScriptSerializer js = new JavaScriptSerializer();
dynamic respObject = js.Deserialize<dynamic>( jsonString );
datos = respObject["Datos"]; // this is data what I need


for ( int i= 0; i <= datos.Length; i++) 
{
    //System.Diagnostics.Debug.Write( "Objeto json: " + d["Nombre"].ToString() );
    System.Diagnostics.Debug.Write( "Objeto json " + i + ":" + datos.ElementAt(i).ToString() );
}

For example, I have this: enter image description here

this is part of json file (I retrieve much more with more properties, but bassically is this)

{
    "Estado": "OK",
    "Datos": [
        {
            "Numero": 61321,
            "Nombre": null,
            "CodigoAgenteVenta": 36,
            "AgenteVenta": null,
            "Actividad": null,
            "CodigoActividad": 40,
            "RazonSocial": "UNIDAD EDUCATIVA SAN LUIS GONZAGA",
            "Ruc": "1791386906001",
        },
        {
            "Numero": 61327,
            "Nombre": null,
            "CodigoAgenteVenta": 148,
            "AgenteVenta": null,
            "Actividad": null,
            "CodigoActividad": 40,
            "RazonSocial": "FUNDACION EDUCACIONAL ALBERTO EINSTEIN",
            "Ruc": "1791376323001",
            
        },
        {
            "Numero": 61365,
            "Nombre": null,
            "CodigoAgenteVenta": 57679,
            "AgenteVenta": null,
            "Actividad": null,
            "CodigoActividad": 46,
            "RazonSocial": "TATASOLUTION CENTER S.A.",
            "Ruc": "1792072328001",
            
        },
        {
            "Numero": 5010098,
            "Nombre": null,
            "CodigoAgenteVenta": 36,
            "AgenteVenta": null,
            "Actividad": null,
            "CodigoActividad": 46,
            "RazonSocial": "MOORE STEPHENS & ASOCIADOS CIA LTDA",
            "Ruc": "0992684038001",
            
        },
        {
            "Numero": 5010113,
            "Nombre": null,
            "CodigoAgenteVenta": 58609,
            "AgenteVenta": null,
            "Actividad": null,
            "CodigoActividad": 46,
            "RazonSocial": "DISMEDSA CIA. LTDA.",
            "Ruc": "0991473998001",
            
        },
        {
            "Numero": 5010114,
            "Nombre": null,
            "CodigoAgenteVenta": 58609,
            "AgenteVenta": null,
            "Actividad": null,
            "CodigoActividad": 46,
            "RazonSocial": "CORPORACION CELESTE CIA. LTDA. CORPACEL",
            "Ruc": "0992426357001",
            
       }
    ],
    "Mensajes": []
}

How can I get for example Ruc value or Nombre Value? Just need a few properties for sending to another application.

My problem is that I retrieve the data in an object[] with deserialization, as you can see in the screen captures and I want to iterate that object to obtain the values.

Please, I hope you can help me.

Best Regards.

urmat abdykerimov
  • 427
  • 1
  • 7
  • 17
angel_neo
  • 333
  • 2
  • 5
  • 22
  • What part of your code can't you figure out? Providing a sample of the JSON would help too. – Matthew Jul 14 '21 at 21:38
  • Why don't you create class objects that represent the json structure and deserialize to that? – Trevor Jul 14 '21 at 21:38
  • Thanks.... I've provided part of the json. My problem is that I retrieve the data in a object[] with deserialization, as you can see in the screen captures and I want iterate that object for obtain the values. – angel_neo Jul 14 '21 at 21:47
  • 3
    9 times out of 10, if you are trying to use object or dynamic to solve of a problem, you are creating 2 more problems. If at all possible, just deserialize to concrete classes – TheGeneral Jul 14 '21 at 22:09
  • https://stackoverflow.com/questions/15455304/deserialize-a-property-as-an-expandoobject-using-json-net – T.S. Jul 14 '21 at 23:04
  • with `dynamic` you probably need `foreach(dynamic x in respObject["Datos"]) a = x["Nombre"];` etc – T.S. Jul 14 '21 at 23:11
  • Thanks a lot Guys... It worked with a foreach with dynamic – angel_neo Jul 15 '21 at 15:14

1 Answers1

0

I could Iterate it with a foreach:

List<Cliente> listClientes = new List<Cliente>();
customer.Data = new List<Datos>();

foreach ( dynamic x in respObject["Datos"] )
{
    var numero = x["Numero"];
    var razonSocial = x["RazonSocial"];
    var ruc = x["Ruc"];
    var nombreRepresentante = x["NombresRepresentante"];
    var apellidoRepresentante = x["ApellidosRepresentante"];
    var region = x["Region"];


    customer.Estado = "OK";
    customer.Data.Add( new Datos() {
        Numero = Convert.ToInt32( numero )
        , RazonSocial = razonSocial
        , Ruc = ruc
        , NombresRepresentante = nombreRepresentante
        , ApellidosRepresentante = apellidoRepresentante
        , Region = region
    });   
}

so I can get a list with all retrieved values

urmat abdykerimov
  • 427
  • 1
  • 7
  • 17
angel_neo
  • 333
  • 2
  • 5
  • 22