Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I got this code where i read a Json file an populate it.
But just want to select an specify field in the json, let's say: lastname.
HttpWebRequest wReq = WebRequest.Create(url) as HttpWebRequest;
           string json = null;
           using (HttpWebResponse wResp = (HttpWebResponse)wReq.GetResponse())
           {
               using (Stream s = wResp.GetResponseStream())
               {
                   using (TextReader tr = new StreamReader(s))
                   {
                       json = tr.ReadToEnd();

                       var resultList = JsonConvert.DeserializeObject<Package[]>(json);
                       PaqueteList.ItemsSource = resultList;
                   }
               }
           }


What I have tried:

I had read many articules, but: do not find the answer
Posted
Updated 2-Aug-22 7:22am
Comments
Afzaal Ahmad Zeeshan 1-Aug-22 22:07pm    
I assume that you want to read this in the UI element where you want to render this.

One way to do this could be to just override the ToString method and return the lastname there. Otherwise, you can also tell the data binding framework to render the lastname in the element.

(I assume this is Xamarin? If so, check the data binding documentation for Xamarin on how you would need to do that for the control that you are using.)
David Crow 3-Aug-22 9:41am    
Can you do something like:
var resultList = JsonConvert.DeserializeObject<Package[]>(json);
string lname = resultList.lastname;

The problem is that JSON data doesn't necessarily contain a single entry for "lastname" - it can contain a whole building full of people, all of whom will have separate last names.

So we can;t tell you "do this" and it'll return "the lastname" because it could be any one of 100 people you are interested in, or it could be a single person's details that the JSON contained.
But the name resultList and the array type teh Deserialize operation refers to implies that there are multiple records in the data!

If you want to access any of the details for a single person, you need to first locate that specific person's data within the collection, and treat it as a separate object in order to get the info you want.

We can't tell you how to do that: we have no access to your Package class, or your JSON data at all!
 
Share this answer
 
Comments
Luis M. Rojas 2-Aug-22 8:13am    
Hello, First i beg you pardon me, when i say: lastname, could be any fields, in my case it is a unique key: tracking number, and here is my class(package):
public class Package
{
[JsonProperty("id_tipo_envio")]
// [JsonConverter(typeof(ParseStringConverter))]
public long IdTipoEnvio { get; set; }

[JsonProperty("peso")]
public string Peso { get; set; }

[JsonProperty("tracking_number")]
public string TrackingNumber { get; set; }

[JsonProperty("contenido")]
public string Contenido { get; set; }

[JsonProperty("guia")]
public string Guia { get; set; }

[JsonProperty("desc_estatus_proceso")]
public string DescEstatusProceso { get; set; }

[JsonProperty("id_estatus_proceso")]
//[JsonConverter(typeof(ParseStringConverter))]
public long IdEstatusProceso { get; set; }

[JsonProperty("fec_creacion")]
public string FecCreacion { get; set; }

[JsonProperty("factura")]
//[JsonConverter(typeof(ParseStringConverter))]
public long Factura { get; set; }

[JsonProperty("trackingestatus")]
public string Trackingestatus { get; set; }

[JsonProperty("colorestatus")]
public string Colorestatus { get; set; }

[JsonProperty("imagenapp")]
public string Imagenapp { get; set; }

[JsonProperty("id_")]
//[JsonConverter(typeof(ParseStringConverter))]
public long Id { get; set; }

[JsonProperty("urlfoto")]
public string Urlfoto { get; set; }

[JsonProperty("facturatext")]
public string Facturatext { get; set; }

[JsonProperty("facturatext2")]
public string Facturatext2 { get; set; }

[JsonProperty("texto1")]
public object Texto1 { get; set; }

[JsonProperty("texto2")]
public object Texto2 { get; set; }

[JsonProperty("mostrar_boton")]
public long MostrarBoton { get; set; }

[JsonProperty("padre")]
//[JsonConverter(typeof(ParseStringConverter))]
public long Padre { get; set; }

[JsonProperty("tipo_envio")]
public string TipoEnvio { get; set; }

[JsonProperty("show_tipo_envio")]
public long ShowTipoEnvio { get; set; }
}
}
Luis M. Rojas 2-Aug-22 12:54pm    
Hello,
Well i just try to use this code:

json = tr.ReadToEnd();
dynamic result = JsonConvert.DeserializeObject(json);
foreach (dynamic paq in result)
{
string string2 = "contenido: " + paq.contenido;
}

But i got this error
Severity	Code	Description	Project	File	Line	Suppression State
Error	CS0656	Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create'	Login	C:\Login\Login\Login\DisplayTracking.xaml.cs	69	Active


but i have the DLL installed in the reference of the project.
I FOUND IT.
Thanks to all of you.
Even i have it on reference, i installed via nudget: and it works.
Thanks again to all of you for your time.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900