Sponser Link

How to read JSON Object Array with Xamarin in Android and IOS

Parsing JSON data in xamarin android and IOS

JSON stands for JavaScript Object Notation. JSON is a light weight format in a text format which can be transform easily in to any object array. In this tutorial, we will learn that how to fetch data from JSON web service and read JSON array object.

The given below data is read from JSON web service. We will create the class for given below data format.

{

  "clsStatus":

          [  {"CardNo1":"a1","CardNo2":"a2","CardNo3":"a3",

              "ColorCode":0,"EmpNo":"15","FlgState":0,

              "FlgStatus":2, "Message":"This is Test Data.",

              "UserName":"NAYCode user"}

          ]

}

We will create Status class for read JSON object then we will create the list class for transform Status class in to array object.

 public class Status

    {

        public string Message { get; set; }

        public int FlgStatus { get; set; }

        public string UserName { get; set; }

        public int FlgState { get; set; }

        public string EmpNo { get; set; }

        public string CardNo1 { get; set; }

        public string CardNo2 { get; set; }

        public string CardNo3 { get; set; }

        public int ColorCode { get; set; }

    }

After create Status class, we will create list type array of status class.

 public class ListStatus

    {

        public List <Status> clsStatus { get; set; }

    }

Install the Newtonsoft into your project and create new class for send http request and read json data.

    public class WSClient

    {

        public async Task Get(string url)

        {

            HttpClient client = new HttpClient();

            var response = await client.GetAsync(url);

            var json = await response.Content.ReadAsStringAsync();

            return JsonConvert.DeserializeObject(json);

        }

    }

Write async method for send request to JSON service and read JSON array.

  public async void RegisterDevice_JSON()

        {

            try

            {

                string EmpNo = txtEmp.Text;

                string str_Mac = GetDeviceInfo();

                int status = 0;

                string str_msg = "";

                string str_UserName = "";

                WSClient client = new WSClient();

                string URL_JSON = "http://testJSONService/SaveRegDetailAttendance/" + str_Mac + "/" + EmpNo ;

                ListStatus result = await client.Get(URL_JSON);

                foreach (Status s in result.clsStatus)

                {

                    lblMsg.Text = s.Message.ToString();

                    status = s.FlgStatus;

                    str_msg = s.Message;

                    str_UserName = s.UserName;

                }

               

            }

            catch (FeatureNotSupportedException fnsEx)

            {

                // Handle not supported on device exception  

                lblMsg.Text = "FeatureNotSupportedException" + fnsEx.Message;

            }

            catch (PermissionException pEx)

            {

                // Handle permission exception  

                lblMsg.Text = "PermissionException" + pEx.Message;

            }

            catch (Exception ex)

            {

                // Unable to get location  

                lblMsg.Text = "Exception: " + ex.Message;

            }

        }