使用JavaScriptSerializer.Deserialize使用双引号反序列化JSON数据

时间:2016-09-01 14:01:13

标签: c# asp.net-mvc-4 json.net special-characters

在JSON数据下面我正在尝试使用JavaScriptSerializer.Deserialize进行反序列化,但是,我正在低于异常。

代码:

var jsonSerialiser = new JavaScriptSerializer();
FCMCasepackVM casepack = jsonSerialiser.Deserialize<FCMCasepackVM>(selectedCasePack);

JSON数据:

 { P_ID:"1478952", P_NM:"BAHLSEN COOKIES WAFER ROLL MILK CHOCOLATE 3.5
 OZ", BYR_ID:191, BYR_NM:"BYR_NM", VDR_ID:48532, VDR_NM:"KEHE FOOD DIST
 INC", CPK_ID:"1478952-12", CPK_DSC:"BAHLSEN COOKIES WAFER ROLL MILK
 CHOCOLAT", VDR_IT_CD_ID:"6398", UPC_ID:7056921950.0,
 CPK_PRI_FLG:"True", CPK_SRC:"PEF", **FCM_RGLT_TYP_CT:"FSV"P"**,
 CPK_P_ORIG_DSC:"", RGLN_EXCL_FLG:"False", BRND_TYP:""  }

异常

  

其他信息:无法转换'System.String'类型的对象   输入'Meijer.MerchProduct.FCM.Models.ViewModels.FCMCasepackVM'

即使我在反序列化之前尝试序列化数据,但获得了不同的异常

代码:

var jsonSerialiser = new JavaScriptSerializer();
selectedCasePack = jsonSerialiser.Serialize(selectedCasePack);
FCMCasepackVM casepack = jsonSerialiser.Deserialize<FCMCasepackVM>(selectedCasePack);

序列化后的JSON数据

 "{ P_ID:\"1478952\", P_NM:\"BAHLSEN COOKIES WAFER ROLL MILK CHOCOLATE
 3.5 OZ\", BYR_ID:191, BYR_NM:\"BYR_NM\", VDR_ID:48532, VDR_NM:\"KEHE FOOD DIST INC\", CPK_ID:\"1478952-12\", CPK_DSC:\"BAHLSEN COOKIES
 WAFER ROLL MILK CHOCOLAT\", VDR_IT_CD_ID:\"6398\",
 UPC_ID:7056921950.0, CPK_PRI_FLG:\"True\", CPK_SRC:\"PEF\",
 FCM_RGLT_TYP_CT:\"FSV\"P\", CPK_P_ORIG_DSC:\"\", RGLN_EXCL_FLG:\"False\", BRND_TYP:\"\"  }"

异常

  

其他信息:传入的对象无效,预期为':'或'}'。   (319):{P_ID:“1478952”,P_NM:“BAHLSEN COOKIES WAFER ROLL MILK   CHOCOLATE 3.5 OZ“,BYR_ID:191,BYR_NM:”BYR_NM“,VDR_ID:48532,   VDR_NM:“KEHE FOOD DIST INC”,CPK_ID:“1478952-12”,CPK_DSC:“BAHLSEN   COOKIES WAFER ROLL MILK CHOCOLAT“,VDR_IT_CD_ID:”6398“,   UPC_ID:7056921950.0,CPK_PRI_FLG:“True”,CPK_SRC:“PEF”,   FCM_RGLT_TYP_CT:“FSV”P“,CPK_P_ORIG_DSC:”“,RGLN_EXCL_FLG:”False“,   BRND_TYP:“”}

任何人都可以帮助我吗?

提前致谢, KARTHIK

2 个答案:

答案 0 :(得分:0)

这个json数据是序列化的吗?在序列化期间,引号应该已转义。

此JSON数据无效,如果您有权访问应用程序序列化此json数据,则应更改该数据以转义字符串中的引号。 (通常所有库在序列化期间自动执行此操作)

更新

您好Karthik,请参阅以下代码,使用newtonsoft的库以JSON格式序列化对象。

公共部分类Form1:表单     {         公共Form1()         {             的InitializeComponent();         }

    private void button1_Click(object sender, EventArgs e)
    {
        var d = new DoTheWork();
        d.SerializeSample();
    }
}


public class Sample
{
    public string Id { get; set; }
    public string Description { get; set; }
}


public class DoTheWork
{

    public string SerializeSample()
    {
        List<Sample> sampleList = new List<Sample>();
        sampleList.Add(new Sample { Id = "1", Description = "Karthik" });
        sampleList.Add(new Sample { Id = "1", Description = "Sujit" });
        sampleList.Add(new Sample { Id = "1", Description = "John\"s" });

        StringBuilder sb = new StringBuilder();
        using (StringWriter sw = new StringWriter(sb))
        {
            JsonSerializer serializer = new JsonSerializer();
            serializer.Serialize(sw, sampleList);

        }
        System.Diagnostics.Debug.Write(sb.ToString());
        return sb.ToString();
    }
}

答案 1 :(得分:0)

您正在使用的JSON无效且不知道FCMCasepackVM的容器对象以及您如何获得JSON,很难分辨出哪里出了问题。下面是一个使用内置的System.Web.Script.Serialization技术和JavaScriptSerializer的简单示例。

public class POC
{
  public int Id { get; set; }
  public string Desc { get; set; }
}

static List<POC> GetPOCOs()
{
  return new List<POC>
  {
      new POC { Id = 1, Desc = "John"},
      new POC { Id = 2, Desc = "Jane" },
      new POC { Id = 3, Desc = "Joey" }
  };
}

static void Main(string[] args)
{
  var pocos = GetPOCOs();

  var serializer = new JavaScriptSerializer();
  var sjson = serializer.Serialize(pocos);
  var djson = serializer.Deserialize<List<POC>>(sjson);

  Console.ReadLine();
}

当我在文本中序列化为JSON时,它应该如下所示:

[{"Id":1,"Desc":"John"},{"Id":2,"Desc":"Jane"},{"Id":3,"Desc":"Joey"}]

或者单个项目可能是:

{"Id":1,"Desc":"John"}

NOT:

{Id:1,Desc:"John"}

通常,密钥对系统都是JSON,但它需要用引号括起来的密钥才能知道它是什么。如果您有任何疑问,请使用在线JSON验证器,如http://jsonlint.com/