none
How to get a particular value in a JSON string RRS feed

  • Question

  • Hello,

    This is very simple Json string 

    {
    
      "@odata.id": "/GrandCanyon/Colarado/USA/World12345678"
    }

    How Can I get only the last I need to get last word World12345678. Currently I am doing a Split('/') and when I do that the first 3 words come nice and crisp but the last one comes with \"\r\n"} all kind of things and in some cases the json string comes in with a trailing '/' too as shown below, to make my life miserable. So please note the solution you advice should work of with and without trailing slash.
    I think what I need is to convert this string to an array and then do array[array.Length -1] should be the word I want. It is always the last. If this can be done with a single line I would be grateful. Thanks 

    {
    
      "@odata.id": "/GrandCanyon/Colarado/USA/World12345678/"
    }

    Thursday, February 8, 2018 7:56 PM

Answers

  • I think that normally you should use some specialised parser. For example, add a reference to System.Runtime.Serialization, then try this example:

    string example =

           @"{

              ""@odata.id"": ""/GrandCanyon/Colarado/USA/World12345678/""

             }";

    var dcjs = new DataContractJsonSerializer( typeof( MyData ) );

    var myData = (MyData)dcjs.ReadObject( new MemoryStream( Encoding.ASCII.GetBytes( example ) ) );

    var value = myData.odataid;

     

    string result = Regex.Match( value, @"(?<=/)[^/]+(?=/?$)" ).Value;

     

    Also add the class:

    [DataContract]

    public class MyData

    {

       [DataMember( Name = "@odata.id" )]

       public string odataid { get; set; }

    }

     

     

     

    But if you prefer a single line, then try a less reliable method:

    string result = Regex.Match( example, @"(?<=/)[^/]+(?=/?"")", RegexOptions.Multiline ).Value;

     



    • Edited by Viorel_MVP Thursday, February 8, 2018 8:31 PM
    • Marked as answer by ananda vardhana Friday, February 9, 2018 1:28 AM
    Thursday, February 8, 2018 8:28 PM
  • Thanks Viorel, I got the thing working

    using System;
    using System.IO;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Json;
    using System.Text;
    using System.Text.RegularExpressions;
    
    public class Program
    {
        static void Main(string[] args)
        {
            string schemaString =
            @"{
    
                ""@odata.id"": ""/GrandCanyon/Colarado/USA/World12345678/""
    
            }";
    
            var dcjs = new DataContractJsonSerializer(typeof(MyData));
            var myData = (MyData)dcjs.ReadObject(new MemoryStream(Encoding.ASCII.GetBytes(schemaString)));
            var value = myData.odataid;
            string[] JSONArray = value.Split('/');
            Console.WriteLine("MemberID = {0}", JSONArray[JSONArray.Length - 2]);
        }
    }
    
    [DataContract]
    public class MyData
    {
        [DataMember(Name = "@odata.id")]
        public string odataid { get; set; }
    }

    thanks a lot

    regards

    ananda






    Thursday, February 8, 2018 9:07 PM

All replies

  • I think that normally you should use some specialised parser. For example, add a reference to System.Runtime.Serialization, then try this example:

    string example =

           @"{

              ""@odata.id"": ""/GrandCanyon/Colarado/USA/World12345678/""

             }";

    var dcjs = new DataContractJsonSerializer( typeof( MyData ) );

    var myData = (MyData)dcjs.ReadObject( new MemoryStream( Encoding.ASCII.GetBytes( example ) ) );

    var value = myData.odataid;

     

    string result = Regex.Match( value, @"(?<=/)[^/]+(?=/?$)" ).Value;

     

    Also add the class:

    [DataContract]

    public class MyData

    {

       [DataMember( Name = "@odata.id" )]

       public string odataid { get; set; }

    }

     

     

     

    But if you prefer a single line, then try a less reliable method:

    string result = Regex.Match( example, @"(?<=/)[^/]+(?=/?"")", RegexOptions.Multiline ).Value;

     



    • Edited by Viorel_MVP Thursday, February 8, 2018 8:31 PM
    • Marked as answer by ananda vardhana Friday, February 9, 2018 1:28 AM
    Thursday, February 8, 2018 8:28 PM
  • Thanks Viorel, I got the thing working

    using System;
    using System.IO;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Json;
    using System.Text;
    using System.Text.RegularExpressions;
    
    public class Program
    {
        static void Main(string[] args)
        {
            string schemaString =
            @"{
    
                ""@odata.id"": ""/GrandCanyon/Colarado/USA/World12345678/""
    
            }";
    
            var dcjs = new DataContractJsonSerializer(typeof(MyData));
            var myData = (MyData)dcjs.ReadObject(new MemoryStream(Encoding.ASCII.GetBytes(schemaString)));
            var value = myData.odataid;
            string[] JSONArray = value.Split('/');
            Console.WriteLine("MemberID = {0}", JSONArray[JSONArray.Length - 2]);
        }
    }
    
    [DataContract]
    public class MyData
    {
        [DataMember(Name = "@odata.id")]
        public string odataid { get; set; }
    }

    thanks a lot

    regards

    ananda






    Thursday, February 8, 2018 9:07 PM