Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to convert this and shown in textbox using xamarin in visual studio.
C#
return JsonConvert.DeserializeObject<string>(result);

this is the complete code.
C#
string url = @"http://api.wunderground.com/api/02e5dd8c34e3e657/geolookup/conditions/forecast/q/Dhaka,Bangladesh.json";
            using (var client = new HttpClient())
            {
                var result =client.GetStringAsync(url);
                return JsonConvert.DeserializeObject<string>(result);
            }
Posted

Note: I don't use Xamarin/Mono, but the code shown here works in Visual Studio 2013, .NET FrameWork 4.5, as long as the project has a reference to NewtonSoft JSON, and I assume it will work, as is, in Xamarin: if it doesn't, then ask Xamarin for support.
Problem with your current code: I suspect that you are not using asynch properly; you can verify that by putting a break-point on the line starting with 'return JsonConvert, and examining the contents of 'result.

Second, it does not make any sense to try and de-serialize JSON into a string: JSON is an object-format and its de-serialize output is an instance of a Class ... in this case a Class containing instances of other Classes containing the data.

You need to supply the deserializer with a proper Class definition/template to "plug-in its data" to an instance of the Class it will create.

Fortunately, it's easy to get the C# class definition for the WeatherUnderground JSON API: go to the JSONToCSharp site [^], and paste in your WU url, press 'Generate, and copy the classes that it produces for you.

Here's exactly what I did to pull down the data using your Bangladesh URI:

0. created a new public Class in Visual Studio which I named WU_JSON
1. went to JSONToCSharp
2. pasted in your url
3. pressed 'Generate and copied the Class definitions created
4. pasted the class definitions into the body of my Class WU_JSON

5. used the following code
C#
// make sure you have these
using System.Linq;
using System.IO;
using System.Net;
using System.Xml;
using Newtonsoft.Json;

private WU_JSON.RootObject Parse_WU_JSON(string targetURI)
{
    Uri uri = new Uri(targetURI);

    var webRequest = WebRequest.Create(uri);
    WebResponse response = webRequest.GetResponse();

    WU_JSON.RootObject wUData = null;

    try
    {
        using (var streamReader = new StreamReader(response.GetResponseStream()))
        {
            var responseData = streamReader.ReadToEnd();
            
            // if you want all the "raw data" as a string
            // just use: responseData.ToString()

            wUData = JsonConvert.DeserializeObject<wu_json.rootobject>(responseData);
        }
    }
    catch (Exception e)
    {
        // error occurred ... ???
    }
    
    return wUData;
}

// sample usage:

string wuUri = @"http://api.wunderground.com/api/02e5dd8c34e3e657/geolookup/conditions/forecast/q/Dhaka,Bangladesh.json";

WU_JSON.RootObject WU_Result = Parse_WU_JSON(wuUri);

if (WU_Result != null)
{
    // do something with the data in WU_Result
}
else
{
    // there was a problem getting the data
}</wu_json.rootobject>
You'll note that in the code above, commented out, is a solution for how to get the complete response from WU in a string.

FootNote: here's the entire WeatherUnderground JSON structure that the JSONToCSharp site generated for me, but note, that the format of this may be subject to change, so it's a good thing to check the site first before using this:
C#
public class WU_JSON
{
    public class Features
    {
        public int geolookup { get; set; }
        public int conditions { get; set; }
        public int forecast { get; set; }
    }

    public class Response
    {
        public string version { get; set; }
        public string termsofService { get; set; }
        public Features features { get; set; }
    }

    public class Station
    {
        public string city { get; set; }
        public string state { get; set; }
        public string country { get; set; }
        public string icao { get; set; }
        public string lat { get; set; }
        public string lon { get; set; }
    }

    public class Airport
    {
        public List<Station> station { get; set; }
    }

    public class Pws
    {
        public List<object> station { get; set; }
    }

    public class NearbyWeatherStations
    {
        public Airport airport { get; set; }
        public Pws pws { get; set; }
    }

    public class Location
    {
        public string type { get; set; }
        public string country { get; set; }
        public string country_iso3166 { get; set; }
        public string country_name { get; set; }
        public string state { get; set; }
        public string city { get; set; }
        public string tz_short { get; set; }
        public string tz_long { get; set; }
        public string lat { get; set; }
        public string lon { get; set; }
        public string zip { get; set; }
        public string magic { get; set; }
        public string wmo { get; set; }
        public string l { get; set; }
        public string requesturl { get; set; }
        public string wuiurl { get; set; }
        public NearbyWeatherStations nearby_weather_stations { get; set; }
    }

    public class Image
    {
        public string url { get; set; }
        public string title { get; set; }
        public string link { get; set; }
    }

    public class DisplayLocation
    {
        public string full { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        public string state_name { get; set; }
        public string country { get; set; }
        public string country_iso3166 { get; set; }
        public string zip { get; set; }
        public string magic { get; set; }
        public string wmo { get; set; }
        public string latitude { get; set; }
        public string longitude { get; set; }
        public string elevation { get; set; }
    }

    public class ObservationLocation
    {
        public string full { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        public string country { get; set; }
        public string country_iso3166 { get; set; }
        public string latitude { get; set; }
        public string longitude { get; set; }
        public string elevation { get; set; }
    }

    public class Estimated
    {
    }

    public class CurrentObservation
    {
        public Image image { get; set; }
        public DisplayLocation display_location { get; set; }
        public ObservationLocation observation_location { get; set; }
        public Estimated estimated { get; set; }
        public string station_id { get; set; }
        public string observation_time { get; set; }
        public string observation_time_rfc822 { get; set; }
        public string observation_epoch { get; set; }
        public string local_time_rfc822 { get; set; }
        public string local_epoch { get; set; }
        public string local_tz_short { get; set; }
        public string local_tz_long { get; set; }
        public string local_tz_offset { get; set; }
        public string weather { get; set; }
        public string temperature_string { get; set; }
        public int temp_f { get; set; }
        public int temp_c { get; set; }
        public string relative_humidity { get; set; }
        public string wind_string { get; set; }
        public string wind_dir { get; set; }
        public int wind_degrees { get; set; }
        public int wind_mph { get; set; }
        public int wind_gust_mph { get; set; }
        public int wind_kph { get; set; }
        public int wind_gust_kph { get; set; }
        public string pressure_mb { get; set; }
        public string pressure_in { get; set; }
        public string pressure_trend { get; set; }
        public string dewpoint_string { get; set; }
        public int dewpoint_f { get; set; }
        public int dewpoint_c { get; set; }
        public string heat_index_string { get; set; }
        public string heat_index_f { get; set; }
        public string heat_index_c { get; set; }
        public string windchill_string { get; set; }
        public string windchill_f { get; set; }
        public string windchill_c { get; set; }
        public string feelslike_string { get; set; }
        public string feelslike_f { get; set; }
        public string feelslike_c { get; set; }
        public string visibility_mi { get; set; }
        public string visibility_km { get; set; }
        public string solarradiation { get; set; }
        public string UV { get; set; }
        public string precip_1hr_string { get; set; }
        public string precip_1hr_in { get; set; }
        public string precip_1hr_metric { get; set; }
        public string precip_today_string { get; set; }
        public string precip_today_in { get; set; }
        public string precip_today_metric { get; set; }
        public string icon { get; set; }
        public string icon_url { get; set; }
        public string forecast_url { get; set; }
        public string history_url { get; set; }
        public string ob_url { get; set; }
        public string nowcast { get; set; }
    }

    public class Forecastday
    {
        public int period { get; set; }
        public string icon { get; set; }
        public string icon_url { get; set; }
        public string title { get; set; }
        public string fcttext { get; set; }
        public string fcttext_metric { get; set; }
        public string pop { get; set; }
    }

    public class TxtForecast
    {
        public string date { get; set; }
        public List<Forecastday> forecastday { get; set; }
    }

    public class Date
    {
        public string epoch { get; set; }
        public string pretty { get; set; }
        public int day { get; set; }
        public int month { get; set; }
        public int year { get; set; }
        public int yday { get; set; }
        public int hour { get; set; }
        public string min { get; set; }
        public int sec { get; set; }
        public string isdst { get; set; }
        public string monthname { get; set; }
        public string monthname_short { get; set; }
        public string weekday_short { get; set; }
        public string weekday { get; set; }
        public string ampm { get; set; }
        public string tz_short { get; set; }
        public string tz_long { get; set; }
    }

    public class High
    {
        public string fahrenheit { get; set; }
        public string celsius { get; set; }
    }

    public class Low
    {
        public string fahrenheit { get; set; }
        public string celsius { get; set; }
    }

    public class QpfAllday
    {
        public double @in { get; set; }
        public int mm { get; set; }
    }

    public class QpfDay
    {
        public double @in { get; set; }
        public int mm { get; set; }
    }

    public class QpfNight
    {
        public double @in { get; set; }
        public int mm { get; set; }
    }

    public class SnowAllday
    {
        public double @in { get; set; }
        public double cm { get; set; }
    }

    public class SnowDay
    {
        public double @in { get; set; }
        public double cm { get; set; }
    }

    public class SnowNight
    {
        public double @in { get; set; }
        public double cm { get; set; }
    }

    public class Maxwind
    {
        public int mph { get; set; }
        public int kph { get; set; }
        public string dir { get; set; }
        public int degrees { get; set; }
    }

    public class Avewind
    {
        public int mph { get; set; }
        public int kph { get; set; }
        public string dir { get; set; }
        public int degrees { get; set; }
    }

    public class Forecastday2
    {
        public Date date { get; set; }
        public int period { get; set; }
        public High high { get; set; }
        public Low low { get; set; }
        public string conditions { get; set; }
        public string icon { get; set; }
        public string icon_url { get; set; }
        public string skyicon { get; set; }
        public int pop { get; set; }
        public QpfAllday qpf_allday { get; set; }
        public QpfDay qpf_day { get; set; }
        public QpfNight qpf_night { get; set; }
        public SnowAllday snow_allday { get; set; }
        public SnowDay snow_day { get; set; }
        public SnowNight snow_night { get; set; }
        public Maxwind maxwind { get; set; }
        public Avewind avewind { get; set; }
        public int avehumidity { get; set; }
        public int maxhumidity { get; set; }
        public int minhumidity { get; set; }
    }

    public class Simpleforecast
    {
        public List<Forecastday2> forecastday { get; set; }
    }

    public class Forecast
    {
        public TxtForecast txt_forecast { get; set; }
        public Simpleforecast simpleforecast { get; set; }
    }

    public class RootObject
    {
        public Response response { get; set; }
        public Location location { get; set; }
        public CurrentObservation current_observation { get; set; }
        public Forecast forecast { get; set; }
    }
}
 
Share this answer
 
v3
Comments
Satheesh Santhosh 30-Oct-14 5:17am    
@BillWood Before trying in xamarin, i just try this in normal C# application in visual studio, there was a error occurred in the RootObject. so please send this complete project file.
Satheesh Santhosh 31-Oct-14 5:58am    
help me @BillWood
BillWoodruff 31-Oct-14 6:14am    
Hi Satheesh, remember that it's important you get the current object structure classes provided by WU for that URI from the JSONToCSharp site linked to. Those classes may be changed by WU at any time.

I'll check the code again tonight, or tomorrow; it worked fine on my machine the day I posted it. cheers, Bill
Satheesh Santhosh 31-Oct-14 10:50am    
okay BillWoodruff. i just now posted a new question. i'm written my own program, it wasn't worked. click here to see
BillWoodruff 1-Nov-14 0:22am    
Hi Sateesh,

Note that what is posted here is specific to the WeatherUnderground site, for reasons I explain in my answer.

I just ran the code posted here again, and it gets the data without error. I'm using Visual Studio 2013, and .NET 4.5. I don't have Mono/Xamarin.

See my answer to your second question for other ideas.

By the way, it's not a good idea to ask people to send code here; most of us are here on QA to help you learn to solve problems for yourself.
Hi Satheesh,

Please see below the code used for converting Student JSON format string to Student Class..in Xamarin.

Class Library

C#
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;


Code for De- Serialization

C#
public static T Deserialize<T>(string json) {
           var obj = Activator.CreateInstance<T>();
           using (var memoryStream = new MemoryStream(Encoding.Unicode.GetBytes(json))) {
               var serializer = new DataContractJsonSerializer(obj.GetType());
               obj = (T) serializer.ReadObject(memoryStream);
               return obj;
           }
       }



The de-serialize function can be called by...
C#
StudentList = Deserialise<list>< Student >> (JsonString);
</list>
 
Share this answer
 
v2

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