Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

How to deserialize and get into model class nested Json ?

$
0
0

This I the json string {"usermachine":[{"employeeid":"EMP002 ","employeename":"EMP002 NAME","machineid":"E0138","machinename":"FOS","iscleaning":1,"isperforming":1,"isverifying":1},{"cInterval":112,"cCleanOperationMaxTime":86400,"cPerformOperationMaxTime":300},{"oSequenceID":2,"oCompletedOperation":1,"oComplOperStartTime":1555384452,"oCompOperEndndTime":1555384652,"oOperationToContinue":2},{"pinterval":[{"pLocationNumber":1,"pLocationName":"TestLoc1","pLocationInterval":12},{"pLocationNumber":2,"pLocationName":"TestLoc2","pLocationInterval":1121}]}]}

Json2Csharp class
public class Pinterval
{
public int pLocationNumber { get; set; }
public string pLocationName { get; set; }
public int pLocationInterval { get; set; }
}

public class Usermachine
{
public string employeeid { get; set; }
public string employeename { get; set; }
public string machineid { get; set; }
public string machinename { get; set; }
public int iscleaning { get; set; }
public int isperforming { get; set; }
public int isverifying { get; set; }
public int? cInterval { get; set; }
public int? cCleanOperationMaxTime { get; set; }
public int? cPerformOperationMaxTime { get; set; }
public int? oSequenceID { get; set; }
public int? oCompletedOperation { get; set; }
public int? oComplOperStartTime { get; set; }
public int? oCompOperEndndTime { get; set; }
public int? oOperationToContinue { get; set; }
public List pinterval { get; set; }
}

public class RootObject
{
public List usermachine { get; set; }
}

This is my models.class
public class Pinterval
{
public int pLocationNumber { get; set; }
public string pLocationName { get; set; }
public int pLocationInterval { get; set; }
}

public class Usermachine
{
    public string employeeid { get; set; }
    public string employeename { get; set; }
    public string machineid { get; set; }
    public string machinename { get; set; }
    public int iscleaning { get; set; }
    public int isperforming { get; set; }
    public int isverifying { get; set; }
    public int cInterval { get; set; }
    public int cCleanOperationMaxTime { get; set; }
    public int cPerformOperationMaxTime { get; set; }
    public int oSequenceID { get; set; }
    public int oCompletedOperation { get; set; }
    public int oComplOperStartTime { get; set; }
    public int oCompOperEndndTime { get; set; }
    public int oOperationToContinue { get; set; }
    public List<Pinterval> pinterval { get; set; }

}

public class UsermachineList
{
    public List<Usermachine> usermachine { get; set; }
}

My Code to retrieve data works with the Usermachine structure but not with the pinterval list.
my code :
var client = new System.Net.Http.HttpClient();

            var response = await client.GetAsync(linkremoved as I am getting a message you have to be around for a while);
            string usersJson = await response.Content.ReadAsStringAsync();
            UsermachineList objUsermachineList = new UsermachineList();
            if (usersJson != "")
            {
                //Converting JSON Array Objects into generic list  
                objUsermachineList = JsonConvert.DeserializeObject<UsermachineList>(usersJson);
                //partially original changed products to productsJason
                // objProductList = JsonConvert.DeserializeAnonymousType(productsJason);
                //// var prds = JsonConvert.DeserializeObject(productsJason);

            }
            //Binding listview with server response    

           // listviewProducts.ItemsSource = objUsermachineList.usermachine;
            foreach (var usm in objUsermachineList.usermachine)
            {
                if (usm != null)
                {
                    string data2Display = usm.employeename.ToString() + " / " + usm.employeeid.ToString() + " / " + usm.machinename.ToString();
                    string pintDisplay = "";
                    foreach (var pint in usm.pinterval) // *** This is where I get issues. pinterval is null
                    {
                        if (pint != null)
                        {
                            pintDisplay = pint.pLocationInterval.ToString() + " / " + pint.pLocationName.ToString() + " / " + pint.pLocationInterval.ToString();

                        }
                        else
                        {
                            break;
                        }
                    }
                    await DisplayAlert("UserMachine", data2Display, "OK");
                    await DisplayAlert("Interval", pintDisplay, "OK");

Hope someone can throw some light and share your ideas.
Thanks in advance


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>