Click here to Skip to main content
15,743,544 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Updated 2-Aug-22 7:22am
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;

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
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
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
// [JsonConverter(typeof(ParseStringConverter))]
public long IdTipoEnvio { get; set; }

public string Peso { get; set; }

public string TrackingNumber { get; set; }

public string Contenido { get; set; }

public string Guia { get; set; }

public string DescEstatusProceso { get; set; }

public long IdEstatusProceso { get; set; }

public string FecCreacion { get; set; }

public long Factura { get; set; }

public string Trackingestatus { get; set; }

public string Colorestatus { get; set; }

public string Imagenapp { get; set; }

public long Id { get; set; }

public string Urlfoto { get; set; }

public string Facturatext { get; set; }

public string Facturatext2 { get; set; }

public object Texto1 { get; set; }

public object Texto2 { get; set; }

public long MostrarBoton { get; set; }

public long Padre { get; set; }

public string TipoEnvio { get; set; }

public long ShowTipoEnvio { get; set; }
Luis M. Rojas 2-Aug-22 12:54pm    
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.

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