简体   繁体   中英

How to parse SQL JSON string in C# in asp.net mvc web api?

I have created web api in asp.net mvc where I am calling usp_JsonPract this SP which is returning JSON string from DB, Now I am facing problem to convert this string on .net mvc web api.

My stored procedure code:

    Create proc [dbo].[usp_JsonPract]
    as
    BeginSelect  category title
        ,[data] = JSON_QUERY(
                    (
                    select din.dishId,din.dishName,din.dishPrice,din.dishImage, din.dishType,
                    JSON_QUERY(dishPriceAndSize, '$.dishPriceAndSize') AS dishPriceAndSize, 
                    JSON_QUERY(JAddOns, '$.addOns') AS addOns, 
                    din.includedEggs, din.dishDescription, din.rating, din.review,din.discount
                    from DishMaster din 
                    where din.category = dout.category 
                    --and dishId in ( 11, 12,13 , 7  )
                    for json path
                    ,INCLUDE_NULL_VALUES
                    )
                  )from DishMaster dout 
    group by category 
    for json path,without_array_wrapper

Stored procedure is returing JSON string that I want to pass to the client. I am using JsonConvert.DeserializeObject(jsonstr); to convert.

My C# code:

    public object SQLJSONPract()
    {
        string jsonstr = string.Empty;
        object o; 
        try
        {
            cmd.CommandText = "usp_JsonPract";
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            adp.Fill(ds);
            var d =  ds.Tables[0].Rows[0][0];
            jsonstr = d.ToString();
            object a = JsonConvert.DeserializeObject(jsonstr);
            return (object)a;
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }

Gives exception as below:

"Unterminated string. Expected delimiter: ". Path 'userDetails[2].data[1].addOns[1].name' , line 59, position 3."

Result sample JSON is like this:

{
"title": "Rice",
"data": [
    {
        "dishId": 11,
        "dishName": "stream rice",
        "dishPrice": 40.0,
        "dishImage": "streamrice.jpg",
        "dishType": "VEG",
        "dishPriceAndSize": [
            {
                "size": "Half",
                "price": 90
            },
            {
                "size": "Full",
                "price": 180
            }
        ],
        "addOns": [
            {
                "name": "Extrachess",
                "price": 25
            },
            {
                "name": "Chess",
                "price": 20
            }
        ],
        "includedEggs": false,
        "dishDescription": "stream rice is delicious in test",
        "rating": 4.5,
        "review": "GOOD",
        "discount": 20
    },
    {
        "dishId": 12,
        "dishName": "stream rice",
        "dishPrice": 40.0,
        "dishImage": "streamrice.jpg",
        "dishType": "VEG",
        "dishPriceAndSize": [
            {
                "size": "Half",
                "price": 90
            },
            {
                "size": "Full",
                "price": 180
            }
        ],
        "addOns": [
            {
                "name": "Extrachess",
                "price": 25
            },
            {
                "name": "Chess",
                "price": 20
            }
        ],
        "includedEggs": false,
        "dishDescription": "stream rice is delicious in test",
        "rating": 4.5,
        "review": "GOOD",
        "discount": 20
    },
    {
        "dishId": 13,
        "dishName": "stream rice",
        "dishPrice": 40.0,
        "dishImage": "streamrice.jpg",
        "dishType": "VEG",
        "dishPriceAndSize": [
            {
                "size": "Half",
                "price": 90
            },
            {
                "size": "Full",
                "price": 180
            }
        ],
        "addOns": [
            {
                "name": "Extrachess",
                "price": 25
            },
            {
                "name": "Chess",
                "price": 20
            }
        ],
        "includedEggs": false,
        "dishDescription": "stream rice is delicious in test",
        "rating": 4.5,
        "review": "GOOD",
        "discount": 20
    },
    {
        "dishId": 7,
        "dishName": "Chicken Biryani",
        "dishPrice": 160.0,
        "dishImage": "ChickenBiryani.jpg",
        "dishType": "NonVEG",
        "dishPriceAndSize": [
            {
                "size": "Half",
                "price": 90
            },
            {
                "size": "Full",
                "price": 180
            }
        ],
        "addOns": [
            {
                "name": "Extrachess",
                "price": 25
            },
            {
                "name": "Chess",
                "price": 20
            }
        ],
        "includedEggs": false,
        "dishDescription": "Special Chicken Biryani For Our Valued Guest",
        "rating": 4.5,
        "review": "GOOD",
        "discount": 20
    }
]}

If any other suggestion for achieving this. Kindly suggest.

Make a file called JsonPract.cs and paste the following classes, in order to create several class that can be mapped to the json that you post in the question:

public class JsonPract
{
    public string title { get; set; }
    public List<Datum> data { get; set; }
}

public class DishPriceAndSize
{
    public string size { get; set; }
    public int price { get; set; }
}

public class AddOn
{
    public string name { get; set; }
    public int price { get; set; }
}

public class Datum
{
    public int dishId { get; set; }
    public string dishName { get; set; }
    public double dishPrice { get; set; }
    public string dishImage { get; set; }
    public string dishType { get; set; }
    public List<DishPriceAndSize> dishPriceAndSize { get; set; }
    public List<AddOn> addOns { get; set; }
    public bool includedEggs { get; set; }
    public string dishDescription { get; set; }
    public double rating { get; set; }
    public string review { get; set; }
    public int discount { get; set; }
}

Then you can deserialize your object as:

JsonPract myDeserializedClass = JsonConvert.DeserializeObject<JsonPract>(myJsonResponse); 

For example:

public JsonPract SQLJSONPract()
    {
        string jsonstr = string.Empty;
        object o; 
        try
        {
            cmd.CommandText = "usp_JsonPract";
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            adp.Fill(ds);
            var d =  ds.Tables[0].Rows[0][0];
            jsonstr = d.ToString();
            JsonPract a = JsonConvert.DeserializeObject<JsonPract>(jsonstr);
            return a;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM