在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:"" }
附加信息:无法转换系统类型的对象。字符串的输入'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饼干威化卷牛奶巧克力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 .饼干威化卷牛奶巧克力",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:"错误",BRND_TYP: "}
提前感谢,恋人
您正在使用的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"}
:
{Id:1,Desc:"John"}
一般来说,一个密钥对系统是JSON的全部,但它需要用引号包围键,以便能够知道它是什么。如果您有疑问,可以使用在线JSON验证器,如http://jsonlint.com/
这个json数据序列化在哪里?在序列化过程中,引号应该进行转义。
这个JSON数据是无效的,如果你有权限访问应用程序序列化这个JSON数据,你应该改变字符串中的转义引号。(通常所有库在序列化过程中都会自动执行)
更新嗨,Karthik,请看下面的代码,使用newtonsoft的库序列化JSON格式的对象。
public partial class Form1: Form{公共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();
}
}