locked
How to Parse JSON Object Array of Data into Views? RRS feed

  • Question

  • User316425 posted

    Hi,

    Well i am knew to Xamarin and confuse of how to parse the JSON object content/array of content into views/component. The purpose of this is that i have a online VPS in the cloud, with MySQL Database there. I put the .php files up and call it using URL to return (in JSON object) the data i query. So, this is what i already know from the web service method:

    1. Request HttpURLConnection (Build Strings for example: http://101.10.30.890/index.php?hello=20?testing=10/)
    2. Then my index.php file return JSON object which i can parse (how to parse the object in it?)
    3. If i'm not mistaken using the Class which extends AsyncTask is the best approach, for example: class FetchTask extends AsyncTask which includes onPreExecute, During, and OnPostExecute method?

    Please correct me if i am wrong, i am just trying to understand how to get the data into my views (Entry and Label for example). I already know how to request the data and return it, what i am missing is how to parse it and put it into my views. Any help is appreciated :)

    Regards, Kevin

    Tuesday, April 25, 2017 1:38 PM

All replies

  • Tuesday, April 25, 2017 1:42 PM
  • User316425 posted

    Thanks @AlessandroCaliaro However, what should i use to hold the return JSON object from the PHP i called?

    And how to assign it to the views? Something like:

    String var1 = JSONobject["the name of the field inside"]?

    Then bind it in the xaml like

    Sorry for the confusing question

    Regards, Kevin

    Tuesday, April 25, 2017 2:36 PM
  • User2148 posted

    You have to Deserialize your Json to an Object X

    then set X to a BindingContext (directly or using ViewModel)

    then Bind X.MyProperty to control's properties

    Tuesday, April 25, 2017 2:53 PM
  • User316425 posted

    Thanks @AlessandroCaliaro.

    I usually use Datatable as the object to hold the result of my query to the database. In the Mobile case can we retrieve the query result in PHP data array, return it in json_encode, then hold it in datatable (do they have datatable in xamarin)?

    Thursday, April 27, 2017 9:17 AM
  • User2148 posted

    I think Datatable is an obj that does not exists in xamarin. Usually when you visualize data in a ListView you should use

    List<YourModel>
    

    or better

    ObservableCollection<YourModel>
    

    for example, if you have a model like this

    public class Order2
    {
        public long orderId { get; set; }
        public string orderStatusCode { get; set; }
        public string orderStatusDescription { get; set; }
        public int serviceProviderId { get; set; }
        public string orderOpeningDate { get; set; }
        public string orderSchedulingDate { get; set; }
        public string orderSchedulingPeriod { get; set; }
        public object orderSettlementDate { get; set; }
        public object orderCancellationDate { get; set; }
    }
    
    public class Order
    {
        public Order2 order { get; set; }
    }
    
    public class RootObject
    {
        public static string MYSTRING { get { return "{ \"orders\": [ { \"order\": { \"orderId\": 7004093603, \"orderStatusCode\": \"CANC\", \"orderStatusDescription\": \"Cancelado\", \"serviceProviderId\": 3649, \"orderOpeningDate\": \"2015-07-07\", \"orderSchedulingDate\": \"2015-07-18\", \"orderSchedulingPeriod\": \"M\", \"orderSettlementDate\": null, \"orderCancellationDate\": null }} ] }"; } }
    
        public List<Order> orders { get; set; }
    }
    

    you can use this to deserialize it

    var r = JsonConvert.DeserializeObject<RootObject>(RootObject.MYSTRING);
    
    Thursday, April 27, 2017 9:21 AM
  • User381799 posted

    Hi @AlessandroCaliaro

    what do you mean by your model how can i creat my model? I have the same problem thanks

    Thursday, January 24, 2019 1:20 PM
  • User2148 posted

    Your "Model" is a class that contains your data. For example you can have a class "User" that has some properties

    public class User()
    {
        public string Name {get;set;}
        public string Age {get;set;}
    }
    
    Thursday, January 24, 2019 1:26 PM
  • User381799 posted

    Hi @AlessandroCaliaro aliaro
    Thank you for your response I have the following json response php_resp = "{\"customer\":\"turkerler\",\"authority\":\"1023\",\"url\":\"http:\/\/gultekinteknik.com.tr\/file\/TURKERLER-LOGO1.png\",\"message\":\"Welcome\"}"

    I need to parse this and using the "Welcome" message to navigate into a new page which contain my data from the php how can i do this? please help me thank in advance

    Friday, January 25, 2019 8:48 AM
  • User76049 posted

    @NewCoder1973 said: Hi @AlessandroCaliaro aliaro
    Thank you for your response I have the following json response php_resp = "{\"customer\":\"turkerler\",\"authority\":\"1023\",\"url\":\"http:\/\/gultekinteknik.com.tr\/file\/TURKERLER-LOGO1.png\",\"message\":\"Welcome\"}"

    I need to parse this and using the "Welcome" message to navigate into a new page which contain my data from the php how can i do this? please help me thank in advance

    @AlessandroCaliaro has given you the answer.

    You can use this site to convert your json into C# classes you need to reserialize your json.

    https://quicktype.io/

    Also refer to the official documentation and then come back if your having issues. https://docs.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/consuming/rest

    Friday, January 25, 2019 9:26 AM
  • User381799 posted

    Thank you both @AlessandroCaliaro and @NMackay for your response I am getting the following response from my url php_resp = "{\"customer\":\"turkerler\",\"authority\":\"1023\",\"url\":\"http:\/\/gultekinteknik.com.tr\/file\/TURKERLER-LOGO1.png\",\"message\":\"Welcome\"}"

    but I am not sure if it is in JSON string format or not? if it is not in json fromat how can i get rid of the backsalashes to make this json formatted?

    Tuesday, January 29, 2019 7:21 AM
  • User2148 posted

    Try this https://app.quicktype.io/#l=cs&r=json2csharp

    Tuesday, January 29, 2019 8:08 AM
  • User330867 posted

    Use Visual Studio itself to create Classes out of JSON:

    Edit -> Paste Special -> Paste JSON as classes

    Tuesday, January 29, 2019 9:35 AM
  • User311196 posted

    @AlessandroCaliaro thanks, you save my day!!!! :)

    Sunday, May 19, 2019 7:30 PM