Click here to Skip to main content
15,040,212 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Using a xamarin cross platform development to i want print a json message from url and print in listview. I write code without error but with some mistakes. I will not work. For just a verfication output only i print the button in the output screen. I will shown but the listview was not print. Please recorrect my code.

C#
static ListView listview;
    static Button button;
    public MainPage()
    {
        listview = new ListView() { RowHeight = 40 };
        button = new Button() { Text = "search Again" };
        var stack = new StackLayout()
        {
            VerticalOptions = LayoutOptions.FillAndExpand,
            Children = { button, listview },
        };
        this.Content = stack;
        GetData();
    }
     async static void GetData()
    {
        WeatherReport res = new WeatherReport();
        try
        {
            string contents;
            string Url = String.Format("http://23.253.66.248:8080/icc/api/venue/search/?                lat=39.540544&lon=-104.866115&appkey=123Chesse&restName=MyMacChennai&organization=MyMacChennai");
            HttpClient hc = new HttpClient();
            contents = await hc.GetStringAsync(Url);
            res = JsonConvert.DeserializeObject<WeatherReport>(contents);
            listview.ItemsSource = res.list;
        }
        catch (System.Exception sysExc)
        {
            // Do something to log this error.
            throw;
        }
    }
 public class WeatherReport
 {
    public WeatherReport()
    {
        list = new List<WeatherReport>();
    }
    [JsonProperty(PropertyName = "cod")]
    public string cod { get; set; }
    [JsonProperty(PropertyName = "message")]
    public string message { get; set; }
    [JsonProperty(PropertyName = "cnt")]
    public int cnt { get; set; }
    [JsonProperty(PropertyName = "list")]
    public List<WeatherReport> list { get; set; }
Posted
Comments
BillWoodruff 31-Oct-14 23:33pm
   
Please edit your post to include what specific error messages you get, and where the errors occur. Use Tags to help identify what technology stack you are using. This is: Windows Forms ?

1 solution

As I explained to you in this solution to your previous question [^]:

You must provide a valid class structure to use JSON de-serialize on Web data, and that structure will almost certainly be different for each web-site that provides JSON you can grab.

And, since any given web-site may decide to change that structure at any time, you should not assume that code you have that works today using today's class structure provided by the site will work in the future.

If I go to the JSONToCSharp web-site I showed you how to use in my previous answer, and generate a class structure for the Chennai website you use here: this is returned
C#
// wrapped in a Class for code clarity
public class MMChennai_JSON
{
    public class ConnectionRespons
    {
        public double distance { get; set; }
        public string hoursOfOperation { get; set; }
        public string address { get; set; }
        public string connection { get; set; }
        public string iomsId { get; set; }
        public string restaurantName { get; set; }
    
        // added by BW : see Notes
        public override string ToString()
        {
            return this.restaurantName;
        }
    }
    
    public class RootObject
    {
        public List<connectionrespons> connectionResponses { get; set; }
        public object message { get; set; }
        public int acknowledge { get; set; }
    }        
}</connectionrespons>
Using the code in the previous solution, I can de-serialize this without error in Visual Studio 2013, .NET 4.5.

Note: I added an over-ridden 'ToString method in the ConnectionRespon which can be used to determine what is displayed in a ListView whose 'DataSource is set to the the List of ConnectionRespon contained in the de-serialized data:
C#
string wuUri =
    
@"http://23.253.66.248:8080/icc/api/venue/search/?lat=39.540544&lon=-104.866115&appkey=123Chesse&restName=MyMacChennai&organization=MyMacChennai";

MMChennai_JSON.RootObject MMChennai_JSON_Result = Parse_MMChennai_JSON(wuUri);

if (MMChennai_JSON_Result != null)
{
    listBox1.DataSource = MMChennai_JSON_Result.connectionResponses;
}
else
{
    // what do you want to do if there is a problem de-serializing the data ?
}
   

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