这是序列化JSON对象的正确返回吗?

本文关键字:返回 序列化 JSON 对象 | 更新日期: 2023-09-27 18:18:31

我试图将我的实体模型序列化为JSON,我不确定返回是否正确。

public IHttpActionResult Get()
{
    using (var MGC = new GC_BranchNameEntities())
    {
        var serializer = new JsonSerializer();
        var jsonIDSA = JsonConvert.SerializeObject(MGC.INV_LIVE_IDSA, Formatting.None);
        try
        {
            return Ok(jsonIDSA);
        }
        catch (Exception e)
        {
            return BadRequest("Error occured when retreiving IDSA data " + e.Message);
        }
    }
}

返回值:

"[{'"id'":1,'"barcode'":'"TestBARCODE'",'"nsr'":0,'"stk_in'":0,'"stk_out'":0,'"sales'":0,'"balance'":1},{'"id'":2,'"barcode'":'"TestBARCODE2'",'"nsr'":0,'"stk_in'":0,'"stk_out'":0,'"sales'":0,'"balance'":1},{'"id'":3,'"barcode'":'"TestBARCODE3'",'"nsr'":0,'"stk_in'":0,'"stk_out'":0,'"sales'":0,'"balance'":1},{'"id'":4,'"barcode'":'"AAA'",'"nsr'":0,'"stk_in'":0,'"stk_out'":0,'"sales'":0,'"balance'":1},{'"id'":5,'"barcode'":'"BBB'",'"nsr'":0,'"stk_in'":0,'"stk_out'":0,'"sales'":0,'"balance'":1}]"

。我没有期待一个带'转义的字符串。我将在另一个项目中对其进行反序列化以使用值。

我在找这样的东西

({"id":1、"条形码":"TestBARCODE"、"nsr":0,"stk_in":0,"stk_out":0,"销售":0,"平衡":1},{"id":2,"条形码":"TestBARCODE2"、"nsr":0,"stk_in":0,"stk_out":0,"销售":0,"平衡":1},{"id":3,"条形码":"TestBARCODE3"、"nsr":0,"stk_in":0,"stk_out":0,"销售":0,"平衡":1},{"id":4,"条形码":"AAA级"、"nsr":0,"stk_in":0,"stk_out":0,"销售":0,"平衡":1},{"id":5,"条形码":"BBB","nsr":0,"stk_in":0,"stk_out":0,"销售":0,"平衡":1})

这是序列化JSON对象的正确返回吗?

正确!反斜杠只是用来转义的,这个json应该可以正常工作。