none
Reading Key from Object RRS feed

  • Question

  • Hi All,

    i have a object as below, which has seperated data in JSON format, how to retrieve key and its values using loop.

    Object retvalObj = JsonConvert.DeserializeObject(retval);

    Monday, September 16, 2019 11:31 AM

All replies

  • Hello,

    Can you provides a valid abbreviated sample of your json data and indicate which parts you want to read then iterate on?


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, September 16, 2019 11:57 AM
    Moderator
  • Hi v k b 

    Please refer to sample code below, don't know whether what you want?

    var JsonString = "[" +
                    "{ \"name\": \"Stephen Cousins\", \"age\": \"21\" }," +
                    "{ \"name\": \"Austin A. Newton\", \"age\": \"22\" }," +
                    "{ \"name\": \"Adam Wilhite\", \"age\": \"23\" }," +
                    "{ \"name\": \"Enis Kurtay YILMAZ\", \"age\": \"24\" }]";
    
                object JsonData = JsonConvert.DeserializeObject(JsonString);
    
                var jObjs = (JArray)JsonData;
    
                foreach (JObject jObj in jObjs.Children())
                {
                    foreach (JToken tkn in jObj.Children())
                    {
                        if (tkn is JProperty)
                        {
                            var prop = tkn as JProperty;
                            Console.WriteLine("{0}={1}", prop.Name, prop.Value);
                        }
                    }
                } 

    Wish code above can help you.

    Monday, September 16, 2019 12:46 PM
  • Hello,

    Use Json.Net NuGet package.

    Class to store data

    public class Class1
    {
        public string name { get; set; }
        public string age { get; set; }
        public override string ToString()
        {
            return $"Name: {name} Age: {age}";
        }
    }

    Read json

    var jsonString = "[" +
                     "{ \"name\": \"Stephen Cousins\", \"age\": \"21\" }," +
                     "{ \"name\": \"Austin A. Newton\", \"age\": \"22\" }," +
                     "{ \"name\": \"Adam Wilhite\", \"age\": \"23\" }," +
                     "{ \"name\": \"Enis Kurtay YILMAZ\", \"age\": \"24\" }]";
    
    var jsonData = JsonConvert.DeserializeObject<List<Class1>>(jsonString);
    
    foreach (var item in jsonData)
    {
        Console.WriteLine(item);
    }

    Results in IDE Output window

    Name: Stephen Cousins Age: 21
    Name: Austin A. Newton Age: 22
    Name: Adam Wilhite Age: 23
    Name: Enis Kurtay YILMAZ Age: 24


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Monday, September 16, 2019 1:32 PM
    Moderator
  • Hi Karen, 

    below is the JSON am getting in a variable

    var retval = "PlannedDeliveryTime": {
    "statusCode": 200,
    "statusMsg": {
    "Status": "failed",
    "Error": "NULL",
    "field": "PLIFZ_ERR"
    }
    },
    "LotSize": {
    "statusCode": 200,
    "statusMsg": {
    "Status": "failed",
    "Error": "Incomplete Lot Size",
    "field": "DISLS_err"
    }

    }

    var jsonData = JsonConvert.DeserializeObject<List<something>>(retval);

    using this code to convert to JSON, but it throwing error, that it cannot be converted to JSON

    Monday, September 16, 2019 2:44 PM
  • Hi Karen, 

    below is the JSON am getting in a variable

    var retval = "PlannedDeliveryTime": {
    "statusCode": 200,
    "statusMsg": {
    "Status": "failed",
    "Error": "NULL",
    "field": "PLIFZ_ERR"
    }
    },
    "LotSize": {
    "statusCode": 200,
    "statusMsg": {
    "Status": "failed",
    "Error": "Incomplete Lot Size",
    "field": "DISLS_err"
    }

    }

    var jsonData = JsonConvert.DeserializeObject<List<something>>(retval);

    using this code to convert to JSON, but it throwing error, that it cannot be converted to JSON

    Well the JSON you have will not compile e.g.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, September 16, 2019 3:00 PM
    Moderator
  • i have a webapi call, the response of the webapi call am saving it in 

    var retval

    something like this am doing 

    var content = response.Content.ReadAsStringAsync().Result;
    return content;

    Monday, September 16, 2019 3:04 PM
  • Right, but what Karen was correctly pointing out is that your syntax is not correct.  A JSON string either has to be a single object, which means it starts with an open brace "{", or an array, which means it starts with an open bracket "[".

    My guess is that you just removed the outer curly braces when you typed your example, but those outer curly braces are absolutely required.  For example

    This would compile, for example:

    var retval = 
    "{"
        "\"PlannedDeliveryTime\": {"
            "\"statusCode\": 200,"
            "\"statusMsg\": {"
                "\"Status\": \"failed\","
                "\"Error\": \"NULL\","
                "\"field\": \"PLIFZ_ERR\""
            "}"
        "},"
        "\"LotSize\": {"
            "\"statusCode\": 200,"
            "\"statusMsg\": {"
                "\"Status\": \"failed\", "
                "\"Error\": \"Incomplete Lot Size\","
                "\"field\": \"DISLS_err\""
            "}"
        "}"
    "}";
    
    
    
    


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Monday, September 16, 2019 4:29 PM
  • Hi Tim,

    {
    "ABCD": {
    "statusCode": 200,
    "statusMsg": {
    "Status": "failed",
    "Error": "NULL",
    "field": "DISPO_ERR"
    }
    },
    "DEF": {
    "statusCode": 500,
    "statusMsg": {
    "Fault": {
    "faultcode": "H2R2H_66000",
    "faultstring": "XML parsing component [Input_S2R] message code: [66017], message body: The following error occurred while running the XML processor: [XML validation error in line:1, column:264, message: no declaration found for element 'ws:MATNR1'.- for more information see file:///informatica_pc/v1012/DataTransformation/CMReports/Tmp/2019-09-16/Input_S2R_25_2_62239_U-_-c-5d7fbc87-f31f/Events.cme].",
    "detail": "NULL"
    }
    }
    },
    "GHI": {
    "statusCode": 500
    },
    "HIJ": {
    "statusCode": 200,
    "statusMsg": {
    "Status": "failed",
    "Error": "NULL",
    "field": "PLIFZ_ERR"
    }
    },
    "TRLT": {
    "statusCode": 500,
    "statusMsg": {
    "Fault": {
    "faultcode": "H2R2H_66000",
    "faultstring": "XML parsing component [Input_S2R] message code: [66017], message body: The following error occurred while running the XML processor: [XML validation error in line:1, column:274, message: empty string encountered.- for more information see file:///informatica_pc/v1012/DataTransformation/CMReports/Tmp/2019-09-16/Input_S2R_27_3_62236_U-_-d-5d7fbc88-f31c/Events.cme].",
    "detail": "NULL"
    }
    }
    },
    "LotSize": {
    "statusCode": 500
    }
    }

    this is the actual string am getting

    Monday, September 16, 2019 4:53 PM
  • Try deserializing with classes as I did prior.

        public class Root
        {
            public ABCD ABCD { get; set; }
            public DEF DEF { get; set; }
            public GHI GHI { get; set; }
            public HIJ HIJ { get; set; }
            public TRLT TRLT { get; set; }
            public Lotsize LotSize { get; set; }
        }
    
        public class ABCD
        {
            public int statusCode { get; set; }
            public Statusmsg statusMsg { get; set; }
        }
    
        public class Statusmsg
        {
            public string Status { get; set; }
            public string Error { get; set; }
            public string field { get; set; }
        }
    
        public class DEF
        {
            public int statusCode { get; set; }
            public Statusmsg1 statusMsg { get; set; }
        }
    
        public class Statusmsg1
        {
            public Fault Fault { get; set; }
        }
    
        public class Fault
        {
            public string faultcode { get; set; }
            public string faultstring { get; set; }
            public string detail { get; set; }
        }
    
        public class GHI
        {
            public int statusCode { get; set; }
        }
    
        public class HIJ
        {
            public int statusCode { get; set; }
            public Statusmsg2 statusMsg { get; set; }
        }
    
        public class Statusmsg2
        {
            public string Status { get; set; }
            public string Error { get; set; }
            public string field { get; set; }
        }
    
        public class TRLT
        {
            public int statusCode { get; set; }
            public Statusmsg3 statusMsg { get; set; }
        }
    
        public class Statusmsg3
        {
            public Fault1 Fault { get; set; }
        }
    
        public class Fault1
        {
            public string faultcode { get; set; }
            public string faultstring { get; set; }
            public string detail { get; set; }
        }
    
        public class Lotsize
        {
            public int statusCode { get; set; }
        }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, September 16, 2019 5:14 PM
    Moderator
  • Hi v k b, 

    Thank you for posting here.

    Accoding to your question, I add the json string to a json file and make a test on my side.

    Here’s my code:

    Main function:

                string path = @"path of the json file";
                string jsonContent = File.ReadAllText(path);
    
                var jsonData = JsonConvert.DeserializeObject<Root>(jsonContent);
    
                Console.ReadLine();

    Classes:

        public class Root
        {
            public ABCD ABCD { get; set; }
            public DEF DEF { get; set; }
            public GHI GHI { get; set; }
            public HIJ HIJ { get; set; }
            public TRLT TRLT { get; set; }
            public Lotsize LotSize { get; set; }
        }
    
        public class ABCD
        {
            public int statusCode { get; set; }
            public Statusmsg statusMsg { get; set; }
        }
    
        public class Statusmsg
        {
            public string Status { get; set; }
            public string Error { get; set; }
            public string field { get; set; }
        }
    
        public class DEF
        {
            public int statusCode { get; set; }
            public Statusmsg1 statusMsg { get; set; }
        }
    
        public class Statusmsg1
        {
            public Fault Fault { get; set; }
        }
    
        public class Fault
        {
            public string faultcode { get; set; }
            public string faultstring { get; set; }
            public string detail { get; set; }
        }
    
        public class GHI
        {
            public int statusCode { get; set; }
        }
    
        public class HIJ
        {
            public int statusCode { get; set; }
            public Statusmsg statusMsg { get; set; }
        }
    
        public class TRLT
        {
            public int statusCode { get; set; }
            public Statusmsg1 statusMsg { get; set; }
        }
    
        public class Lotsize
        {
            public int statusCode { get; set; }
        }

    Result of my test:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 18, 2019 7:46 AM
    Moderator