-3
{
    "StudentInformation": {
        "rollNumber": null,
        "isClassLeader": false,
        "result": "Pass"

    },

    "CollegeInformation": {
        "allClass": ["A", "B"],
        "currencyAccepted": "INR",
        "calendarDates": [],
        "currencyCode": "INR",
        "collegeCode": null,
        "hasBulidingFundPrices": false,
        "hasHostel": false,
        "hasSecurityFares": false

    },


    "Collegetrips": [{
        "tripsdate": [{
                "departureTripDate": "2017-08-15 00:00:00",
                    "Places": [{
                            "destination": "Bombay",
                            "price": [{
                                "priceAmount": 1726
                                    }]
                                }]
                        }]
             }]     
}

In the above json file i need to retrieve only "priceAmount": 1726. Please anyone suggest how can able to achieve?

CodeNotFound
  • 22,153
  • 10
  • 68
  • 69

3 Answers3

0

I use: http://json2csharp.com/ to get a class representing the Json Object.

public class StudentInformation
{
    public object rollNumber { get; set; }
    public bool isClassLeader { get; set; }
    public string result { get; set; }
}

public class CollegeInformation
{
    public List<string> allClass { get; set; }
    public string currencyAccepted { get; set; }
    public List<object> calendarDates { get; set; }
    public string currencyCode { get; set; }
    public object collegeCode { get; set; }
    public bool hasBulidingFundPrices { get; set; }
    public bool hasHostel { get; set; }
    public bool hasSecurityFares { get; set; }
}

public class Price
{
    public int priceAmount { get; set; }
}

public class Place
{
    public string destination { get; set; }
    public List<Price> price { get; set; }
}

public class Tripsdate
{
    public string departureTripDate { get; set; }
    public List<Place> Places { get; set; }
}

public class Collegetrip
{
    public List<Tripsdate> tripsdate { get; set; }
}

public class JsonResponse
{
    public StudentInformation StudentInformation { get; set; }
    public CollegeInformation CollegeInformation { get; set; }
    public List<Collegetrip> Collegetrips { get; set; }
}

After that I use Newtonsoft.Json to fill the Class:

using Newtonsoft.Json;

namespace GitRepositoryCreator.Common
{
    class JObjects
    {
        public static string Get(object p_object)
        {
            return JsonConvert.SerializeObject(p_object);
        }
        internal static T Get<T>(string p_object)
        {
            return JsonConvert.DeserializeObject<T>(p_object);
        }
    }
}

You can call it like that:

JsonResponse jsonClass = JObjects.Get<JsonResponse>(stringJson);

string stringJson = JObjects.Get(jsonClass);

PS: If your json variable name is no valid C# name you can fix that like this:

public class Exception
{
   [JsonProperty(PropertyName = "$id")]
   public string id { get; set; }
   public object innerException { get; set; }
   public string message { get; set; }
   public string typeName { get; set; }
   public string typeKey { get; set; }
   public int errorCode { get; set; }
   public int eventId { get; set; }
}
RoJaIt
  • 451
  • 3
  • 10
0

You can use System.Web.Script.Serialization (you need to add a reference to System.Web.Extensions):

dynamic json = new JavaScriptSerializer()
    .DeserializeObject(jsonString);

decimal price = json["Collegetrips"][0]
    ["tripsdate"][0]
    ["Places"][0]
    ["price"][0]
    ["priceAmount"]; // 1726

Note that you can pretty much traverse the json in this manner using indexes and key names.

TVOHM
  • 2,740
  • 1
  • 19
  • 29
0

Hi try this,

public void Main()
            {
                string sJSON = "{\"StudentInformation\": {\"rollNumber\": null,\"isClassLeader\": false,\"result\": \"Pass\"},\"CollegeInformation\": {\"allClass\": [\"A\", \"B\"],\"currencyAccepted\": \"INR\",\"calendarDates\": [],\"currencyCode\": \"INR\",\"collegeCode\": null,\"hasBulidingFundPrices\": false,\"hasHostel\": false,\"hasSecurityFares\": false},\"Collegetrips\": [{\"tripsdate\": [{\"departureTripDate\": \"2017-08-15 00:00:00\",\"Places\": [{\"destination\": \"Bombay\",\"price\": [{\"priceAmount\": 1726}]}]}]}]}";
                Rootobject obj = Newtonsoft.Json.JsonConvert.DeserializeObject<Rootobject>(sJSON);
                Price price = obj.Collegetrips.Select(ct =>
                {
                    var r = ct.tripsdate.Select(td =>
                    {
                        var r1 = td.Places.Select(p =>
                        {
                            Price itemPrice = p.price.FirstOrDefault();
                            return itemPrice;
                        }).FirstOrDefault();

                        return r1;

                    }).FirstOrDefault();
                    return r;
                }).FirstOrDefault();

                if (price != null)
                    Console.Write(price.priceAmount);
                else
                    Console.Write("Not Found!");


            }


    public class Rootobject
    {
        public Studentinformation StudentInformation { get; set; }
        public Collegeinformation CollegeInformation { get; set; }
        public Collegetrip[] Collegetrips { get; set; }
    }

    public class Studentinformation
    {
        public object rollNumber { get; set; }
        public bool isClassLeader { get; set; }
        public string result { get; set; }
    }

    public class Collegeinformation
    {
        public string[] allClass { get; set; }
        public string currencyAccepted { get; set; }
        public object[] calendarDates { get; set; }
        public string currencyCode { get; set; }
        public object collegeCode { get; set; }
        public bool hasBulidingFundPrices { get; set; }
        public bool hasHostel { get; set; }
        public bool hasSecurityFares { get; set; }
    }

    public class Collegetrip
    {
        public Tripsdate[] tripsdate { get; set; }
    }

    public class Tripsdate
    {
        public string departureTripDate { get; set; }
        public Place[] Places { get; set; }
    }

    public class Place
    {
        public string destination { get; set; }
        public Price[] price { get; set; }
    }

    public class Price
    {
        public int priceAmount { get; set; }
    }
C.Fasolin
  • 313
  • 1
  • 4
  • 19