locked
JsonConvert.DeserializeObject question? RRS feed

  • Question

  • User-1104215994 posted

    Hi,

    I have a web API which calls another web API and deserialize the response, save into database etc. Now I am calling another web API and trying to deserialize the response. But the response is not like the first web API so I am trying to manipulate/look like the response of the second web API to the first one. 

    Here is my response class:

    public class GameResponse
        {
            public int Id { get; set; }
            public string referenceId { get; set; }
            public string productCode { get; set; }
            public int quantity { get; set; }
            public string version { get; set; }
            public string signature { get; set; }
            public string ApplicationCode { get; set; }
            public string validatedToken { get; set; }
            public DateTime? responseDateTime { get; set; } = DateTime.Now;
            public string initiationResultCode { get; set; }
            public string companyToken { get; set; }
            public string estimateUnitPrice { get; set; }
            public string currency { get; set; }
        }

    Here is my second web API response JSON:

    {
        "jsonrpc": "2.0",
        "id": "ED35BA0C-DBE1-4C57-A865-78BDFED686E1",
        "result": {
            "orderId": "40c80a8c-a603-40b9-bf9f-fdd4f579a08b",
            "status": "FULFILLED",
            "items": [
                {
                    "sku": "PUBGM_60UC",
                    "codes": "Code-xxxxxx5"
                }
            ]
        }
    }

    I wonder if I can match the id of the JSON response with referenceId?

    var gameResponse = JsonConvert.DeserializeObject<GameResponse>(htmlResponse);

    Wednesday, January 1, 2020 5:58 PM

Answers

  • User753101303 posted

    Hi,

    You have attributes such as https://www.newtonsoft.com/json/help/html/JsonPropertyName.htm but if the structure is really different you'll likely have to read data using a class and "transfert' data using code to the other class.

    From a technical point of view it seems both have basically nothing in common. Are you sure it makes sense to start with ?

    Edit: or do you mean you consider to apply the https://www.jsonrpc.org/ specification to your own API?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 1, 2020 9:48 PM
  • User665608656 posted

    Hi cenk1536,

    As @PatriceSc provided the reference link, you can add JsonProperty in your class Field to match "id" in your JSON response.

     public class GameResponse
        {
            public int Id { get; set; }
    
            [JsonProperty("id")]
            public string referenceId { get; set; }
            public string productCode { get; set; }
            public int quantity { get; set; }
            public string version { get; set; }
            public string signature { get; set; }
            public string ApplicationCode { get; set; }
            public string validatedToken { get; set; }
            public DateTime? responseDateTime { get; set; } = DateTime.Now;
            public string initiationResultCode { get; set; }
            public string companyToken { get; set; }
            public string estimateUnitPrice { get; set; }
            public string currency { get; set; }
        }

    In fact, the json string you provided is very different from your class structure, so some properties in the json string cannot be obtained and displayed.

    If you can change your class design, then I suggest you create a class based on the return value of your json string like this :

        public class GameResponse
        {
            public string jsonrpc { get; set; }
            public string id { get; set; }
            public Result result { get; set; }
        }
        public class Result
        {
            public string orderId { get; set; }
            public string status { get; set; }
            public List<Item> items { get; set; }
        }
    
        public class Item
        {
            public string sku { get; set; }
            public string codes { get; set; }
        }
    

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 2, 2020 2:14 AM

All replies

  • User753101303 posted

    Hi,

    You have attributes such as https://www.newtonsoft.com/json/help/html/JsonPropertyName.htm but if the structure is really different you'll likely have to read data using a class and "transfert' data using code to the other class.

    From a technical point of view it seems both have basically nothing in common. Are you sure it makes sense to start with ?

    Edit: or do you mean you consider to apply the https://www.jsonrpc.org/ specification to your own API?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 1, 2020 9:48 PM
  • User665608656 posted

    Hi cenk1536,

    As @PatriceSc provided the reference link, you can add JsonProperty in your class Field to match "id" in your JSON response.

     public class GameResponse
        {
            public int Id { get; set; }
    
            [JsonProperty("id")]
            public string referenceId { get; set; }
            public string productCode { get; set; }
            public int quantity { get; set; }
            public string version { get; set; }
            public string signature { get; set; }
            public string ApplicationCode { get; set; }
            public string validatedToken { get; set; }
            public DateTime? responseDateTime { get; set; } = DateTime.Now;
            public string initiationResultCode { get; set; }
            public string companyToken { get; set; }
            public string estimateUnitPrice { get; set; }
            public string currency { get; set; }
        }

    In fact, the json string you provided is very different from your class structure, so some properties in the json string cannot be obtained and displayed.

    If you can change your class design, then I suggest you create a class based on the return value of your json string like this :

        public class GameResponse
        {
            public string jsonrpc { get; set; }
            public string id { get; set; }
            public Result result { get; set; }
        }
        public class Result
        {
            public string orderId { get; set; }
            public string status { get; set; }
            public List<Item> items { get; set; }
        }
    
        public class Item
        {
            public string sku { get; set; }
            public string codes { get; set; }
        }
    

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 2, 2020 2:14 AM
  • User-1104215994 posted

    My web API is a middleware, I designed the request/response for the first web API. Now I can not change it so I have to manipulate the response.

    Thursday, January 2, 2020 5:22 AM
  • User665608656 posted

    Hi cenk1536,

    If you must use JsonConvert.DeserializeObject to deserialize json and you cannot change the model, then I suggest you change the json returned in your webapi.

    I suggest that you organize the json returned in your webapi according to the model you accept to ensure that the fields match, otherwise the deserialization cannot receive the value you passed.

    Or I suggest that when you currently receive the json string, use the dynamic type to accept, rather than a specific GameResponse model.

    You can refer to this link :

    Deserialize JSON into C# dynamic object?

    Best Regards,

    YongQing.

    Thursday, January 2, 2020 8:14 AM
  • User-474980206 posted

    in you middleware you should define the new classes, and deserialize to them. then copy the values to a new instance of the desired class.

    Thursday, January 2, 2020 3:01 PM