Tag: json

如何使用Json.NET StringEscapeHandling.EscapeNonAscii

可能重复: 将StringEscapeHandling.EscapeNonAscii与Json.NET一起使用 最新版本的Json.NET(4.5.11)包含StringEscapeHandling.EscapeNonAscii ,它可以转义所有非ASCII字符。 但是,我无法弄清楚如何使用它。 这些文档似乎没有我能找到的任何示例(仅发布一个发布说明)。 有人可以使用EscapeNonAscii显示将对象序列化为JSON字符串的EscapeNonAscii吗?

使用ReferenceLoopHandling.Ignore序列化循环相关的ISerializable对象时抛出StackOverflowException

我有一个遗留应用程序,它使用二进制序列化来保存数据。 现在我们想使用Json.net 4.5来序列化数据,而不需要对现有类进行太多更改。 事情很顺利,直到我们打了一个循环的依赖类。 有没有解决这个问题的解决方法? 示例代码如下所示 [Serializable] class Department : ISerializable { public Employee Manager { get; set; } public string Name { get; set; } public Department() { } public Department( SerializationInfo info, StreamingContext context ) { Manager = ( Employee )info.GetValue( “Manager”, typeof( Employee ) ); Name = ( string )info.GetValue( “Name”, typeof( […]

Newtonsoft.Json SerializeObject没有转义反斜杠

鉴于代码: dynamic foo = new ExpandoObject(); foo.Bar = “something”; string json = Newtonsoft.Json.JsonConvert.SerializeObject(foo); 输出如下: “{\”Bar\”:\”something\”}” 调试大型json文档时很难阅读 – 使用Newtonsoft.Json的内置function(不是正则表达式或可能破坏事物的黑客攻击)有没有办法使输出成为一个带有valie的字符串: {Bar: “something”}

如何在ASP.NET MVC中进行测试时访问JsonResult数据

我在C#mvc控制器中有这个代码: [HttpPost] public ActionResult Delete(string runId) { if (runId == “” || runId == null) { return this.Json(new { error = “Null or empty params” }); } try { int userId = (int)Session[“UserId”]; int run = Convert.ToInt32(runId); CloudMgr cloud = new CloudMgr(Session); cloud.DeleteRun(userId, run); return this.Json(new { success = true }); } catch (Exception ex) […]

如何在Json.Net中跳过IEnumerable类型的默认JavaScript数组序列化?

一些实现IEnumerable的自定义类型不一定具有后备集合。 它们可以动态生成,例如使用’yield’或LINQ。 这是一个例子: public class SOJsonExample { public class MyCustomEnumerable : IEnumerable<KeyValuePair> { public List Keys { get; set; } public List Values { get; set; } public MyCustomEnumerable() { Keys = new List { 1, 2, 3 }; Values = new List { 0.1f, 0.2f, 0.3f }; } public IEnumerator<KeyValuePair> GetEnumerator() { var kvps […]

使用C#返回JSON,如PHP json_encode

在PHP中返回一些JSON,我会这样做: return json_encode(array(‘param1’=>’data1′,’param2’=>’data2’)); 我如何以最简单的方式在C#ASP.NET MVC3中做等效的操作?

RestSharp发布一个JSON对象

我想用RestSharp发布以下JSON: {“UserName”:”UAT1206252627″, “SecurityQuestion”:{ “Id”:”Q03″, “Answer”:”Business”, “Hint”:”The answer is Business” }, } 我认为我很接近,但我似乎正在努力使用SecurityQuestion (API正在抛出一个错误,说参数丢失,但它没有说明哪一个) 这是我到目前为止的代码: var request = new RestRequest(“api/register”, Method.POST); request.RequestFormat = DataFormat.Json; request.AddParameter(“UserName”, “UAT1206252627”); SecurityQuestion securityQuestion = new SecurityQuestion(“Q03”); request.AddParameter(“SecurityQuestion”, request.JsonSerializer.Serialize(securityQuestion)); IRestResponse response = client.Execute(request); 我的安全问题类看起来像这样: public class SecurityQuestion { public string id {get; set;} public string answer {get; set;} public string hint {get; […]

将JSON反序列化为多个属性

我正在针对返回JSON数据的第三方API编程,但格式可能有点奇怪。 某些属性可以是对象(包含Id属性),也可以是字符串(对象的Id)。 例如,以下两个都是有效的: { ChildObject: ‘childobjectkey1’ } 和 { ChildObject: { Id: ‘childobjectkey1’, // (other properties) } } 我正在尝试使用JSON.net将其反序列化为强类型类,但到目前为止还没有太多运气。 我最好的想法是将它序列化为两个属性,一个是字符串,另一个是对象,并为每个属性使用自定义JsonConverter来允许变量行为: public abstract class BaseEntity { public string Id { get; set; } } public class ChildObject : BaseEntity { } public class MyObject { [JsonProperty(“ChildObject”)] [JsonConverter(typeof(MyCustomIdConverter))] public string ChildObjectId { get; set; } [JsonProperty(“ChildObject”)] [JsonConverter(typeof(MyCustomObjectConverter))] […]

有没有办法JSON.NET-序列化List 的子类,还有额外的属性?

好的,我们正在使用Newtonsoft的JSON.NET产品,我非常喜欢。 但是,我有一个简单的类结构用于层次结构位置,看起来大致像这样…… public class Location { public string Name{ get; set; } public LocationList Locations{ get; set; } } // Note: LocationList is simply a subclass of a List // which then adds an IsExpanded property for use by the UI. public class LocationList : List { public bool IsExpanded{ get; set; } } public […]

在没有第三方框架的情况下从HttpWebResponse反序列化JSON的方法

我试图避免依赖开源或第三方库(如Json.NET)来解析来自HttpWebResponse的传入JSON。 为什么? 因为更多地依赖开源框架来帮助实现,你的应用程序必须依赖这些依赖关系…我不喜欢我的应用程序依赖很多库,如果可能的话。 我可以使用像Enterprise Library这样的东西,因为它支持MS,但我正在使用更多的开源库。 无论如何,我试图找出解析.NET 3.5中传入的JSON服务器端的最佳方法。 我知道这会得到很多响应,我甚至使用.NET 3.5 JavaScriptSerializer将数据序列化为JSON,但现在我正试图找出最好和最简单的方法来做反向,不用了使用第三方/开源框架来帮助解决这个问题。