Deserialize Json to list and save to database

35 views Asked by At

Want to deserialize Json in list and save to database.

public class APICallsResponse
{

    public string CODE { get; set; }
    public string STATUS { get; set; }
    public string MESSAGE { get; set; }
    public string TOTAL_RECORDS { get; set; }
    public string RESPONSE { get; set; }


    public string UNIQUE_QUERY_ID { get; set; }
    public string QUERY_TYPE { get; set; }
    public string QUERY_TIME { get; set; }
    public string SENDER_NAME { get; set; }
    public string SENDER_MOBILE { get; set; }
    public string SENDER_EMAIL { get; set; }
    public string SUBJECT { get; set; }
    public string SENDER_COMPANY { get; set; }
    public string SENDER_ADDRESS { get; set; }
    public string SENDER_CITY { get; set; }
    public string SENDER_STATE { get; set; }
    public string SENDER_PINCODE { get; set; }
    public string SENDER_COUNTRY_ISO { get; set; }
    public string SENDER_MOBILE_ALT { get; set; }
    public string SENDER_PHONE { get; set; }
    public string SENDER_PHONE_ALT { get; set; }
    public string SENDER_EMAIL_ALT { get; set; }
    public string QUERY_PRODUCT_NAME { get; set; }
    public string QUERY_MESSAGE { get; set; }
    public string QUERY_MCAT_NAMEE { get; set; }
    public string CALL_DURATIONE { get; set; }
    public string RECEIVER_MOBILEE { get; set; }
}

Response:

{
    "CODE": 200,
    "STATUS": "SUCCESS",
    "MESSAGE": "",
    "TOTAL_RECORDS": 66,
    "RESPONSE": [
        {
            "UNIQUE_QUERY_ID": "2472779",
            "QUERY_TYPE": "W",
            "QUERY_TIME": "2023-06-21 13:02:06",
            "SENDER_NAME": "Golu  k",
            "SENDER_MOBILE": "+91-789790000",
            "SENDER_EMAIL": "",
            "SUBJECT": "Requirement for",
            "SENDER_COMPANY": "",
            "SENDER_ADDRESS": "Varanasi, Uttar Pradesh",
            "SENDER_CITY": "Varanasi",
            "SENDER_STATE": "Uttar Pradesh",
            "SENDER_PINCODE": "",
            "SENDER_COUNTRY_ISO": "IN",
            "SENDER_MOBILE_ALT": "",
            "SENDER_PHONE": "",
            "SENDER_PHONE_ALT": "",
            "SENDER_EMAIL_ALT": "",
            "QUERY_PRODUCT_NAME": "Mini",
            "QUERY_MESSAGE": "I want to buy Mini. Kindly send me price and other details.
<br>Probable Requirement Type : Business Use<br>",
            "QUERY_MCAT_NAME": "Mini",
            "CALL_DURATION": "",
            "RECEIVER_MOBILE": ""
        }

Below item = code does not work to deserialize. Want to insert one by one to table.

There is response tag want the data from that tag

1

There are 1 answers

0
Power Mouse On

lets start from basics.

option 1: (read json string as is)

  1. I would assume, that class you show id a DTO class matching the database record.in this case you need another set of classes which will match JSON structure which will deserialize it to object correctly

public class APICallsResponse
{
    public string CODE { get; set; }
    public string STATUS { get; set; }
    public string MESSAGE { get; set; }
    public string TOTAL_RECORDS { get; set; }
    public List<RESPONSE> RESPONSE { get; set; }
}

public class RESPONSE
{
    public string UNIQUE_QUERY_ID { get; set; }
    public string QUERY_TYPE { get; set; }
    public string QUERY_TIME { get; set; }
    public string SENDER_NAME { get; set; }
    public string SENDER_MOBILE { get; set; }
    public string SENDER_EMAIL { get; set; }
    public string SUBJECT { get; set; }
    public string SENDER_COMPANY { get; set; }
    public string SENDER_ADDRESS { get; set; }
    public string SENDER_CITY { get; set; }
    public string SENDER_STATE { get; set; }
    public string SENDER_PINCODE { get; set; }
    public string SENDER_COUNTRY_ISO { get; set; }
    public string SENDER_MOBILE_ALT { get; set; }
    public string SENDER_PHONE { get; set; }
    public string SENDER_PHONE_ALT { get; set; }
    public string SENDER_EMAIL_ALT { get; set; }
    public string QUERY_PRODUCT_NAME { get; set; }
    public string QUERY_MESSAGE { get; set; }
    public string QUERY_MCAT_NAMEE { get; set; }
    public string CALL_DURATIONE { get; set; }
    public string RECEIVER_MOBILEE { get; set; }
}

which will deserialise to object correctly

string json = "{\"CODE\":200,\"STATUS\":\"SUCCESS\",\"MESSAGE\":\"\",\"TOTAL_RECORDS\":66,\"RESPONSE\":[{\"UNIQUE_QUERY_ID\":\"2472779\",\"QUERY_TYPE\":\"W\",\"QUERY_TIME\":\"2023-06-21 13:02:06\",\"SENDER_NAME\":\"Golu  k\",\"SENDER_MOBILE\":\"+91-789790000\",\"SENDER_EMAIL\":\"\",\"SUBJECT\":\"Requirement for\",\"SENDER_COMPANY\":\"\",\"SENDER_ADDRESS\":\"Varanasi, Uttar Pradesh\",\"SENDER_CITY\":\"Varanasi\",\"SENDER_STATE\":\"Uttar Pradesh\",\"SENDER_PINCODE\":\"\",\"SENDER_COUNTRY_ISO\":\"IN\",\"SENDER_MOBILE_ALT\":\"\",\"SENDER_PHONE\":\"\",\"SENDER_PHONE_ALT\":\"\",\"SENDER_EMAIL_ALT\":\"\",\"QUERY_PRODUCT_NAME\":\"Mini\",\"QUERY_MESSAGE\":\"I want to buy Mini. Kindly send me price and other details.< br > Probable Requirement Type : Business Use<br>\",\"QUERY_MCAT_NAME\":\"Mini\",\"CALL_DURATION\":\"\",\"RECEIVER_MOBILE\":\"\"}]}";
    JsonConvert.DeserializeObject<APICallsResponse>(json).Dump();

enter image description here

then you can map and populate your DTO object from deserialized one and store to DB using your Data layer functionality (DAL)

Option 2 (different approach):

i would assume you need just a response object in this case you can read a RESPONSE JToken directly into RESPONSE class

this is an examle:

void Main()
{
    string json = "{\"CODE\":200,\"STATUS\":\"SUCCESS\",\"MESSAGE\":\"\",\"TOTAL_RECORDS\":66,\"RESPONSE\":[{\"UNIQUE_QUERY_ID\":\"2472779\",\"QUERY_TYPE\":\"W\",\"QUERY_TIME\":\"2023-06-21 13:02:06\",\"SENDER_NAME\":\"Golu  k\",\"SENDER_MOBILE\":\"+91-789790000\",\"SENDER_EMAIL\":\"\",\"SUBJECT\":\"Requirement for\",\"SENDER_COMPANY\":\"\",\"SENDER_ADDRESS\":\"Varanasi, Uttar Pradesh\",\"SENDER_CITY\":\"Varanasi\",\"SENDER_STATE\":\"Uttar Pradesh\",\"SENDER_PINCODE\":\"\",\"SENDER_COUNTRY_ISO\":\"IN\",\"SENDER_MOBILE_ALT\":\"\",\"SENDER_PHONE\":\"\",\"SENDER_PHONE_ALT\":\"\",\"SENDER_EMAIL_ALT\":\"\",\"QUERY_PRODUCT_NAME\":\"Mini\",\"QUERY_MESSAGE\":\"I want to buy Mini. Kindly send me price and other details.< br > Probable Requirement Type : Business Use<br>\",\"QUERY_MCAT_NAME\":\"Mini\",\"CALL_DURATION\":\"\",\"RECEIVER_MOBILE\":\"\"}]}";
    JObject.Parse(json).SelectToken("RESPONSE").ToObject<List<RESPONSE>>().Dump();
}

public class RESPONSE
{
    public string UNIQUE_QUERY_ID { get; set; }
    public string QUERY_TYPE { get; set; }
    public string QUERY_TIME { get; set; }
    public string SENDER_NAME { get; set; }
    public string SENDER_MOBILE { get; set; }
    public string SENDER_EMAIL { get; set; }
    public string SUBJECT { get; set; }
    public string SENDER_COMPANY { get; set; }
    public string SENDER_ADDRESS { get; set; }
    public string SENDER_CITY { get; set; }
    public string SENDER_STATE { get; set; }
    public string SENDER_PINCODE { get; set; }
    public string SENDER_COUNTRY_ISO { get; set; }
    public string SENDER_MOBILE_ALT { get; set; }
    public string SENDER_PHONE { get; set; }
    public string SENDER_PHONE_ALT { get; set; }
    public string SENDER_EMAIL_ALT { get; set; }
    public string QUERY_PRODUCT_NAME { get; set; }
    public string QUERY_MESSAGE { get; set; }
    public string QUERY_MCAT_NAMEE { get; set; }
    public string CALL_DURATIONE { get; set; }
    public string RECEIVER_MOBILEE { get; set; }
}

but you will not have data (we will ignore the top set of columns):

    public string CODE { get; set; }
    public string STATUS { get; set; }
    public string MESSAGE { get; set; }
    public string TOTAL_RECORDS { get; set; }

enter image description here