How to extract key values from a JSON String.

vishnusharma.8852vishnusharma.8852 USMember ✭✭
edited January 2014 in Xamarin.iOS

In my iOS code in Xamarin I am getting a json string in response from the Rest API and its structure is like bellow.

{

"base": "gdps stations",
"id": 1269515,
"name": "Jaipur",
"weather": [
{
"description": "haze",
"icon": "50n",
"id": 721,
"main": "Haze"
}
],
"wind": {
"deg": 70,
"speed": 2.1
}
}

How can I use this string and extract values for the some key ? May be I can save this string in a dictionary and use that to get the key value. (Please specify which name space I need to use and what assembly I need to add )

Posts

  • RockMeAmadeusRockMeAmadeus USMember ✭✭

    You should check out RestSharp from the component store:

    http://components.xamarin.com/view/restsharp

  • RockMeAmadeusRockMeAmadeus USMember ✭✭

    With that said, also http://json2csharp.com/ comes in quite handy in combination with RestSharp at least for starters...

  • DaveHuntDaveHunt USMember ✭✭✭✭✭

    Have a look at Introduction to Web Services, specifically the section titled "Options for consuming RESTful data".

  • DannyCDannyC US ✭✭✭✭

    @vishnusharma.8852 would also recommend the Json.NET component http://components.xamarin.com/view/json.net. You could go the easy route using JObject.Parse () or if your data format is static you can create a class that you can easily deserialize with JsonConvert.DeserializeObject.

    This code below was created with your json @ http://json2csharp.com.

    
    public class Weather
    {
        public string description { get; set; }
        public string icon { get; set; }
        public int id { get; set; }
        public string main { get; set; }
    }
    
    public class Wind
    {
        public int deg { get; set; }
        public double speed { get; set; }
    }
    
    public class RootObject
    {
        public string @base { get; set; }
        public int id { get; set; }
        public string name { get; set; }
        public List weather { get; set; }
        public Wind wind { get; set; }
    }
    
  • I am new with .Net or C#

    I updated my code something like bellow.

    System;

    System.Drawing;

    System.IO;

    MonoTouch.Foundation;

    MonoTouch.UIKit;

    System.Collections.Generic;

    Newtonsoft.Json;

    System.Web;

    // and in my class

            string json = @"{'name': 'testname' }";
            WeatherReport wr = JsonConvert.DeserializeObject<WeatherReport>(json);
            Console.WriteLine("name is {0}",wr.name);
    

    public class WeatherReport
    {
    public string name { get; set; }
    }

    But when I run the app I get exception

    Could not load file or assembly 'System.Dynamic.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.

    I tried to add "System.Dynamic.Runtime" but there is nothing like this in Xamarin.

  • DannyCDannyC US ✭✭✭✭

    Did you add the component from the store? There are some discussions on a similar thread: http://forums.xamarin.com/discussion/9011/system-typeloadexception/p1

  • RockMeAmadeusRockMeAmadeus USMember ✭✭
    string json = @"{'name': 'testname' }";
    

    why the ?

  • vishnusharma.8852vishnusharma.8852 USMember ✭✭
    edited January 2014

    I come to know that System.Web.Helpers.Json is in System.Web.Helpers.dll is not the part of the MonoTouch or Mono for android so we can not use this in our platform specific projects. But there are several other options that can be used for this task.

    1. System.Json from System.Json.dll it would need to add assembly System.Json in your project.

    2. DataContractJsonSerializer from System.ServiceModel.web.dll assembly System.ServiceModel

    3. 3rd Party JSON Lib like JSON.net for Monotouch namespace Newtonsoft.Json [Link(https://github.com/ayoung/Newtonsoft.Json) download the code from this link and build it and you will find the lib in the bin folder then add the assembly in your project.

    I used the 3rd one option and some code is bellow.

      public string ObjToJSON(WeatherReport oObject)   
    

    {
    string sJSON = "";
    sJSON = Newtonsoft.Json.JsonConvert.SerializeObject(oObject);
    return sJSON;
    }

    public WeatherReport JSONToObj(String JSONString)
    {
    WeatherReport deseri = JsonConvert.DeserializeObject(JSONString);
    return deseri;
    }

  • @RockMeAmadeus sorry it was typo, I'm from iOS and in that string start with @string .

Sign In or Register to comment.