简体   繁体   中英

How to remove first and last square brackets from JSON from query string web api c#

How to remove only response SQUARE BRACKET FROM result - response: []

public HttpResponseMessage GetDetail(int id)
    {

        var result = new Dictionary<string, object>();
        EDetail EDetailobj = db.EDetails.FirstOrDefault(P => P.DetailID == id);
        if (EDetailobj != null)
        {
            result.Add("Status", "ok");

            var responseResult = db.EDetails
                .Where(x => x.DetailID == id)
                .Select(x => new
                {
                    x.DetailID ,
                    x.DetailName,
                    x.Qty,
                    x.Price,
                });
            result.Add("Response", responseResult);
            return Request.CreateResponse(HttpStatusCode.OK, result);
        }
        else
        {
            result.Add("Status", "failure");
            return Request.CreateResponse(result);
        }
    }

{
  "status": "ok",
  "response": [
    {
      "detailID": 1,
      "detailName": "whiteshirt",
      "qty": 12,
      "price": 21.0,
    }
  ]
}

In your DB query, you're returning a list of results that match the query x.DetailID == id . True, there's probably only one result in it, but that doesn't change the fact that Where always returns a collection , not a single object.

So now you're returning, as part of your result, as list containing a single item. This list is serialized into JSON exactly as you see it - as a JSON Array (the square brackets) containing a single object.

If you want to avoid that, don't return a collection - return a single object:

var responseResult = db.EDetails
            .Where(x => x.DetailID == id)
            .Select(x => new
            {
                x.DetailID ,
                x.DetailName,
                x.Qty,
                x.Price,
            })
            .SingleOrDefault();

simply you can use First() or FirstOrDefault()

 var responseResult = db.EDetails
                .Where(x => x.DetailID == id)
                .Select(x => new
                {
                    x.DetailID ,
                    x.DetailName,
                    x.Qty,
                    x.Price,
                }).FirstOrDefault();

You should take care of following points while choosing Single, SingleOrDefault, First and FirstOrDefault:

When you want an exception to be thrown if the result set contains many records, use Single or SingleOrDefault.

When you want a default value is returned if the result set contains no record, use SingleOrDefault.

When you always want one record no matter what the result set contains, use First or FirstOrDefault.

When you want a default value if the result set contains no record,

FirstOrDefault usually perform faster as compared SingleOrDefault, since these iterate the collection until they find the first match. While SingleOrDefault iterate the whole collection to find one single match.

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