locked
convert JSON to list RRS feed

  • Question

  • User-1100884601 posted

    Hi,

    i am getting JSON in this format 

     {{
    "extId": -1,
    "hsId": 1084238908,
    "name": "Server"
    }}

    I want to convert it to list using c#. 

    Thanks in advance,

    Wednesday, November 6, 2019 7:47 AM

Answers

  • User61956409 posted

    Hi udal_cse,

    I am getting  JSON from one of the source and want to convert it to list.

    omehow its not working because of double {{ 

    Based on your example data, we can find it is not valid JSON format data, if you'd like to convert it to a list in C#, you should modify the incoming data format first, like below.

    var res = string.Format("[{0}]", jsondata.Substring(1, jsondata.Length - 2));
    
    var mytest_list = JsonConvert.DeserializeObject<List<MyTest>>(res);

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 6, 2019 9:25 AM

All replies

  • User61956409 posted

    Hi udal_cse,

    want to convert it to list using c#

    You can try:

    public class MyTest
    {
        public int extId { get; set; }
        public int hsId { get; set; }
        public string name { get; set; }
    }
    var mytest_list = new List<MyTest>() {
        new MyTest() {
            extId = -1,
            hsId = 1084238908,
            name = "Server"
        }
    };

    With Regards,

    Fei Han

    Wednesday, November 6, 2019 8:00 AM
  • User-1100884601 posted

    Hi Fie,

    Thanks for your reply,

     I want to convert the JSON to list using c# .  I am getting  JSON from one of the source and want to convert it to list.

    json format is 

    {{
    "extId": -1,
    "hsId": 1084238908,
    "name": "Server"
    }}

    I want to use this JSON value in my c# code.  but somehow its not working because of double {{ 

    Wednesday, November 6, 2019 8:14 AM
  • User61956409 posted

    Hi udal_cse,

    I am getting  JSON from one of the source and want to convert it to list.

    omehow its not working because of double {{ 

    Based on your example data, we can find it is not valid JSON format data, if you'd like to convert it to a list in C#, you should modify the incoming data format first, like below.

    var res = string.Format("[{0}]", jsondata.Substring(1, jsondata.Length - 2));
    
    var mytest_list = JsonConvert.DeserializeObject<List<MyTest>>(res);

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 6, 2019 9:25 AM