none
deserialize json stirng to variables RRS feed

  • Question

  • I get this json return that I need to deserialze:

    {
      "odata.metadata":"http://ausmtsapp01.epicorsaas.com/SaaS203/api/v1/BaqSvc/cLotInfo/$metadata#Epicor.DynamicQuery.QueryResults&$select=PartLot_PartLotDescription,PODetail_LineDesc","value":[
        {
          "PartLot_PartLotDescription":"SL1900301122019","PODetail_LineDesc":"Angle - Steel - A36 - Plain Finish - 3\" x 3\" x 3/8\" x 20' Lg."
        }
      ]
    }


    I have this code to deserialize the PODetail_LineDesc part into a variable and I keep getting null return. What am I doing wrong?

                dynamic jsonDe = JsonConvert.DeserializeObject(response.Content);

                string PODetail_LineDesc = jsonDe.PODetail_LineDesc;
                frmDescription.Text = PODetail_LineDesc;

    MeToo_

    Tuesday, October 15, 2019 9:19 PM

Answers

  • Hi MeToo_, 

    Thank you for posting here.

    For your question, I make a test on my side, and it successfully deserialize json string to variables.

    You can refer to the following code.

    class Program
        {
            static void Main(string[] args)
            {
    
                string jsonString = @"{'odata.metadata':'...',
                                                'value':[
                                                {
                                                        'PartLot_PartLotDescription':'SL1900301122019',
                                                        'PODetail_LineDesc':'Angle - Steel - A36 - Plain Finish - 3 x 3 x 3/8 x 20 Lg.'
                                                }
                                                        ]
                                    }";
                dynamic json = JsonConvert.DeserializeObject<class1>(jsonString);
                Console.WriteLine($"{ json.value[0].PartLot_PartLotDescription}");
                Console.ReadLine();
            }
        }
        class class1
        {
            public string metadata { get; set; }
            public  List<Value> value { get; set; }
        }
        class Value
        {
            public string PartLot_PartLotDescription { get; set; }
            public string PODetail_LineDesc { get; set; }
        }
    

    Result of the test.

    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.

    • Marked as answer by MeToo_ Wednesday, October 16, 2019 10:15 AM
    Wednesday, October 16, 2019 6:05 AM
    Moderator

All replies

  • I would say you may have to put a debug breakpoint the after the dynamic dynamic object line and use Quickwatch and look at the content of the dynamic object. You may find that the property names are case sensitive in the dynamic object.


    Tuesday, October 15, 2019 9:54 PM
  • Check an example:

    string content = @"
           {
           ""odata.metadata"":""http://ausmtsapp01.epicorsaas.com/SaaS203/api/v1/BaqSvc/cLotInfo/$metadata#Epicor.DynamicQuery.QueryResults&$select=PartLot_PartLotDescription,PODetail_LineDesc"",
                  ""value"": [
                         {
                               ""PartLot_PartLotDescription"":""SL1900301122019"",
                               ""PODetail_LineDesc"":""Angle - Steel - A36 - Plain Finish - 3\"" x 3\"" x 3/8\"" x 20' Lg.""
     
                         }
                  ]
           }";
     
    dynamic jsonDe = JsonConvert.DeserializeObject( content );
     
    string PODetail_LineDesc = jsonDe.value[0].PODetail_LineDesc;
     

    It is also possible to de-serialise these data to a class.

    Wednesday, October 16, 2019 4:47 AM
  • Hi MeToo_, 

    Thank you for posting here.

    For your question, I make a test on my side, and it successfully deserialize json string to variables.

    You can refer to the following code.

    class Program
        {
            static void Main(string[] args)
            {
    
                string jsonString = @"{'odata.metadata':'...',
                                                'value':[
                                                {
                                                        'PartLot_PartLotDescription':'SL1900301122019',
                                                        'PODetail_LineDesc':'Angle - Steel - A36 - Plain Finish - 3 x 3 x 3/8 x 20 Lg.'
                                                }
                                                        ]
                                    }";
                dynamic json = JsonConvert.DeserializeObject<class1>(jsonString);
                Console.WriteLine($"{ json.value[0].PartLot_PartLotDescription}");
                Console.ReadLine();
            }
        }
        class class1
        {
            public string metadata { get; set; }
            public  List<Value> value { get; set; }
        }
        class Value
        {
            public string PartLot_PartLotDescription { get; set; }
            public string PODetail_LineDesc { get; set; }
        }
    

    Result of the test.

    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.

    • Marked as answer by MeToo_ Wednesday, October 16, 2019 10:15 AM
    Wednesday, October 16, 2019 6:05 AM
    Moderator
  • I would do something like this (Almost identical to other replies but in it's own function)

            private void handleResponse(string json)
            {
    
                dynamic jsonDe = JsonConvert.DeserializeObject(json);
                string PODetail_LineDesc = jsonDe["value"][0]["PODetail_LineDesc"].ToString();
                string PartLot_PartLotDescription = jsonDe["value"][0]["PartLot_PartLotDescription"].ToString();
                
                //Use Values Here
    
            }


    Thursday, October 17, 2019 4:23 PM