Tag: json

如何返回带有unicode字符的json结果转义为\ u1234

我正在实现一个返回json结果的方法,如: public JsonResult MethodName(Guid key){ var result = ApiHelper.GetData(key); //Data is stored in db as varchar with åäö return Json(new { success = true, data = result },”application/json”, Encoding.Unicode, JsonRequestBehavior.AllowGet ); } 显示的结果: {“success”:true,”data”:[{“Title”:”Here could be characters like åäö”,”Link”:”http://www.url.com/test”,…}, 但我想显示它像: {“success”:true,”data”:[{“Title”:”Here could be characters like \u00e5\u00e4\u00f6″,”Link”:”http:\/\/www.url.com\/test”,…}, 我怎么能做到这一点? 我可以转换它,解析它或更改web.config中的responseEncoding以使其显示unicode字符吗?

运行正确的SerializeObject方法?

我在我的项目中添加了json dll并尝试序列化对象: ProductType itemToEdit = new ProductType(“Name”); string jsonString = JsonConvert.SerializeObject(itemToEdit); 出现错误: Newtonsoft.Json.DLL中出现“System.MissingMethodException”类型的exception,但未在用户代码中处理 附加信息:找不到方法:’Boolean System.Runtime.Serialization.DataContractAttribute.get_IsReference()’。 但项目正确构建。

将未命名的json数组反序列化为c#中的对象

想知道如何在c#中反序列化以下字符串: “[{\”access_token\”:\”thisistheaccesstoken\”}]” 如果json是:我知道怎么做 “{array=[{\”access_token\”:\”thisistheaccesstoken\”}]}” 我这样做: public class AccessToken { public string access_token {get;set;} public DateTime expires { get; set; } } public class TokenReturn { public List tokens { get; set; } } JavaScriptSerializer ser = new JavaScriptSerializer(); TokenReturn result = ser.Deserialize(responseFromServer); 但是没有那个arrays名称,我不确定。 有什么建议? 谢谢!

将JSON反序列化为Object时出错

我需要转换从REST API获取的JSON数据,并将它们转换为CSV以进行某些分析。 问题是JSON数据不一定遵循相同的内容,因此我无法定义映射类型。 这已经成为一项挑战,占用了我太多的时间。 我已经创建了一些代码,但当然它不起作用,因为它会抛出exception var data = JsonConvert.DeserializeObject<List>(jsonData); 错误是: 附加信息:无法将当前JSON对象(例如{“name”:“value”})反序列化为类型’System.Collections.Generic.List`1 [System.Object]’,因为该类型需要JSON数组(例如[1] ,2,3]正确反序列化。 要修复此错误,请将JSON更改为JSON数组(例如[1,2,3])或更改反序列化类型,使其成为普通的.NET类型(例如,不是像整数这样的基本类型,而不是类似的集合类型可以从JSON对象反序列化的数组或List。 JsonObjectAttribute也可以添加到类型中以强制它从JSON对象反序列化。 路径’数据’,第2行,第10位。 请让我知道我能做些什么才能实现这一目标。 数据样本就是这样,数据字段可以经常更改,例如可以在第二天添加新字段,因此我没有自由创建.Net类来映射数据。 { “data”: [ { “ID”: “5367ab140026875f70677ab277501bfa”, “name”: “Happiness Initiatives – Flow of Communication/Process & Efficiency”, “objCode”: “PROJ”, “percentComplete”: 100.0, “plannedCompletionDate”: “2014-08-22T17:00:00:000-0400”, “plannedStartDate”: “2014-05-05T09:00:00:000-0400”, “priority”: 1, “projectedCompletionDate”: “2014-12-05T08:10:21:555-0500”, “status”: “CPL” }, { “ID”: “555f452900c8b845238716dd033cf71b”, “name”: “UX Personalization Think […]

如何在JSON输出上强制ISO格式“YYYY-MM-DDThh:mm:ss.sss”?

我有一个ASP .NET WebApi2 api,它以JSON格式返回一些时间戳。 时间戳有毫秒的分辨率。 通常我得到这种格式的时间戳,很好”YYYY-MM-DDThh:mm:ss.sss” 不幸的是,如果时间戳碰巧用整秒编码日期,则输出格式为”YYYY-MM-DDThh:mm:ss” (注意缺少的.sss “) 如何在JSON输出上一直强制ISO格式”YYYY-MM-DDThh:mm:ss.sss” ?

DateTime.ParseExact:“字符串未被识别为有效的DateTime”

我正在尝试从javascript应用程序发送JSON请求,并在我的SQL DB中创建此对象(HourRegistration),但由于某种原因,我无法将日期从json解析为有效的DateTime。 来自JS的JSON日期 hourRegistration.Date = “12/08/2015”; 所以dd/MM/yyyy 我试图像这样解析: Date = DateTime.ParseExact(hourRegistration.Date, “dd/MM/yyyy”, CultureInfo.InvariantCulture) 整个方法 public void CreateHours(HourRegistrationDTO hourRegistration) { DAO.Instance.HourRegistration.Add(new HourRegistration() { Date = DateTime.ParseExact(hourRegistration.Date, “dd/MM/yyyy”, CultureInfo.InvariantCulture), Cust_ID = hourRegistration.Cust_id, Login_ID = hourRegistration.Login_id, Hours = hourRegistration.Hours, Comment = hourRegistration.Comment }); DAO.Instance.SaveChanges(); } 截图 ToCharArray() 我真的不知道为什么这不起作用。 据我所知,这应该有用吗?

Sitecore页面编辑器中保存返回错误

在页面编辑器中保存页面时出现错误。不知何故,当我从演示文稿>详细信息编辑页面并在页面编辑器中显示它时工作正常..错误日志在下面这里.. ERROR After parsing a value an unexpected character was encountered: {. Path ‘scLayout’, line 38, position 85. Exception: Newtonsoft.Json.JsonReaderException Message: After parsing a value an unexpected character was encountered: {. Path ‘scLayout’, line 38, position 85. Source: Newtonsoft.Json at Newtonsoft.Json.JsonTextReader.ParsePostValue() at Newtonsoft.Json.JsonTextReader.ReadInternal() at Newtonsoft.Json.JsonTextReader.Read() at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String […]

RESTful Web服务返回XML而不是JSON

我有这个简单的Web服务,现在它只是查看部件号是否为A123456789然后它返回一个型号。 这将由将连接到数据库以检查partno的逻辑替换,然后返回实际的型号。 但此时我只需要它返回一些虚拟JSON数据。 然而,当我使用Fiddler并查看http:// localhost:PORT / Scan / Model / A123456789的web broswer中的调用时,它返回此 Model: CVS-1679 但是当我在同一个URI的fiddler中进行GET时,我得到了 “Model: CVS-1679” 仅在textview选项卡下。 当我将ResponseFormat设置为JSON时,为什么它以XML(在Fiddler中的浏览器和文本中)而不是JSON返回? 我的代码: [WebGet(UriTemplate = “Model/{partno}”, ResponseFormat = WebMessageFormat.Json)] public string Model(string partno) { if (partno == “A123456789”) { string modelno = “CVS-1679”; return “Model: ” + modelno; } else { string modelno = “CVS-1601”; return “Model: ” […]

使用JSON.NET与ExpandableObjectConverter的问题

我定义了以下类: Public Class Vector3 Public Property X As Double Public Property Y As Double Public Property Z As Double Public Overrides Function ToString() As String Return String.Format(“({0}, {1}, {2})”, Format(X, “0.00”), Format(Y, “0.00”), Format(Z, “0.00”)) End Function End Class 使用DataContractJsonSerializer我按预期收到以下JSON: { “Vector”: { “X”: 1.23, “Y”: 4.56, “Z”: 7.89 } } 但是,JSON.NET产生: { “Vector”: “(1.23, […]

arrays的反序列化不支持Type

我正在尝试对数组进行解法,但我一直遇到错误。 JavaScriptSerializer jsonSerializer = new JavaScriptSerializer(); Profiles thingy = jsonSerializer.Deserialize(fileContents); 这是给我错误的代码: Type is not supported for deserialization of an array. 这就是我的JSON看起来的样子: [ { “Number”: 123, “Name”: “ABC”, “ID”: 123, “Address”: “ABC” } ]