locked
How do we fetch specific tag value from JSON File RRS feed

  • Question

  • User-1024101449 posted

    I want to get some tag value from my json file. How do i get.

    below my JSON File : 

    {
        "name": "content",
    
         "properties": {
            
    	"emp_name": "RAPHEL-AZ542",
            
    	"m_emp_id": "54854545asb4578",
            
    	"rendition": 0,
            
    	"full_format": "zip",
            
    	"format": "2701222480000203",
            
    	"full_content_size": 10266328,
            
    	"set_time": "2012-06-28T10:16:19.000+0000",
            
    	"i_vstamp": 0,
            
    	"mime_type": "application/x-zip-compressed",
            
    	"dos_extension": "zip",
            
    	"format_name": "zip",
            
    	"parent_id": [
                
    		"54854545asb4578"
            
    	],
            
    	"page": [
                
    		0
            
    	],
            
    	"page_modifier": [
                
    		null
            
    	]
        
         },
        
         "links": [
            
    	{
                
    		"rel": "self",
                
    		"href": "http://testserver.mnet.com.intranet/tm-prerest/repo/ADKS/objects/54854545asb45784/vumcontents/onlycontent"
            
    	},
            
    	{
                
    		"rel": "enclosure",
                
    		"title": "XYZ",
                
    		"href": "http://teserrver.m.sadfasdfsaf.asdfdasfdasdasf"
            
    	},
            
    	{
                
    		"rel": "http://identifiers.kumsc.com/linkrel/content-media",
                
    		"title": "XYS",
                
    		"href": "http://teserrver.m.sadfasdfsaf.asdfdasfdasdasf"  ==> Output tag
                 
    	},
            
    	{
            
    		"rel": "parent",
                
    		"href": "http://testserver.mnet.com.intranet/tm-prerest/repo/ADKS/objects/54854545asb45784"
            
    	}
        
        ]
    
    }

    i want to get href value in the "links" tag

    Final Output as mentioned below from links  "href"

    strvalue = http://teserrver.m.sadfasdfsaf.asdfdasfdasdasf

    C# Code is below :

     JObject o1 = JObject.Parse(File.ReadAllText(@"c:\Empdata\Emp101.json"));
    
                    string json = string.Empty;
                    using (StreamReader file
                             = File.OpenText(@"c:\Empdata\Emp101.json"))
                    using (JsonTextReader reader = new JsonTextReader(file))
                    {
                        JObject o1 = (JObject)JToken.ReadFrom(reader);
    
                        json = o1.ToString();
                    }

    Thursday, May 30, 2019 8:45 AM

Answers

  • User-821857111 posted

    The easiest way is to create a C# class that mimics the JSON structure. You can do that in VS: https://dailydotnettips.com/did-you-know-you-can-automatically-create-classes-from-json-or-xml-in-visual-studio/

    You will end up with something like this:

    public class Rootobject
    {
        public string name { get; set; }
        public Properties properties { get; set; }
        public Link[] links { get; set; }
    }
    
    public class Properties
    {
        public string emp_name { get; set; }
        public string m_emp_id { get; set; }
        public int rendition { get; set; }
        public string full_format { get; set; }
        public string format { get; set; }
        public int full_content_size { get; set; }
        public DateTime set_time { get; set; }
        public int i_vstamp { get; set; }
        public string mime_type { get; set; }
        public string dos_extension { get; set; }
        public string format_name { get; set; }
        public string[] parent_id { get; set; }
        public int[] page { get; set; }
        public object[] page_modifier { get; set; }
    }
    
    public class Link
    {
        public string rel { get; set; }
        public string href { get; set; }
        public string title { get; set; }
    }

    Than you can use Json.NET to convert the JSON to something that you can work with in C#:

    var root = JsonConvert.DeserializeObject<Rootobject>(File.ReadAllText(@"path_to_your_json.json"));
    var link = root.links.FirstOrDefault(l => l.rel == "http://identifiers.kumsc.com/linkrel/content-media");

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 30, 2019 11:46 AM

All replies

  • User-821857111 posted

    The easiest way is to create a C# class that mimics the JSON structure. You can do that in VS: https://dailydotnettips.com/did-you-know-you-can-automatically-create-classes-from-json-or-xml-in-visual-studio/

    You will end up with something like this:

    public class Rootobject
    {
        public string name { get; set; }
        public Properties properties { get; set; }
        public Link[] links { get; set; }
    }
    
    public class Properties
    {
        public string emp_name { get; set; }
        public string m_emp_id { get; set; }
        public int rendition { get; set; }
        public string full_format { get; set; }
        public string format { get; set; }
        public int full_content_size { get; set; }
        public DateTime set_time { get; set; }
        public int i_vstamp { get; set; }
        public string mime_type { get; set; }
        public string dos_extension { get; set; }
        public string format_name { get; set; }
        public string[] parent_id { get; set; }
        public int[] page { get; set; }
        public object[] page_modifier { get; set; }
    }
    
    public class Link
    {
        public string rel { get; set; }
        public string href { get; set; }
        public string title { get; set; }
    }

    Than you can use Json.NET to convert the JSON to something that you can work with in C#:

    var root = JsonConvert.DeserializeObject<Rootobject>(File.ReadAllText(@"path_to_your_json.json"));
    var link = root.links.FirstOrDefault(l => l.rel == "http://identifiers.kumsc.com/linkrel/content-media");

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 30, 2019 11:46 AM
  • User-1024101449 posted

    NA

    Thursday, May 30, 2019 12:06 PM
  • User-1024101449 posted

    done..thanks...

    Thursday, May 30, 2019 12:51 PM