Tag: json

带接口的JsonConverter

我有一个来自客户端的对象,并自动从Web Api 2反序列化。 现在我的模型的一个属性有问题。 此属性“CurrentField”属于IField类型,此接口有2种不同的实现。 这是我的模特(只是假人) public class MyTest { public IField CurrentField {get;set;} } public interface IField{ string Name {get;set;} } public Field1 : IField{ public string Name {get;set;} public int MyValue {get;set;} } public Field2 : IField{ public string Name {get;set;} public string MyStringValue {get;set;} } 我试图创建一个自定义JsonConverter来找出我的客户端的对象是什么类型(Field1或Field2),但我只是不知道如何。 调用我的转换器,当我调用var obj = JObject.load(reader)时,我可以看到该对象; 但我怎样才能知道它是什么类型的? 我做不了类似的事情 […]

如何使用Json.Net序列化/反序列化具有其他属性的自定义集合

我有一个自定义集合(实现IList),它有一些自定义属性,如下所示: class FooCollection : IList { private List _foos = new List(); public string Bar { get; set; } //Implement IList, ICollection and IEnumerable members… } 当我序列化时,我使用以下代码: JsonSerializerSettings jss = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto }; string serializedCollection = JsonConvert.SerializeObject( value , jss ); 它正确地序列化和反序列化所有收集项目; 但是,不考虑FooCollection类中的任何额外属性。 无论如何将它们包含在序列化中?

如何从3.5 asmx Web服务获取JSON响应

我有以下方法: using System.Web.Services; using System.Web.Script.Services; using System.Web.Script.Serialization; using Newtonsoft.Json; using System.Collections; [WebService(Namespace = “http://tempuri.org/”)] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] //[System.ComponentModel.ToolboxItem(false)] [System.Web.Script.Services.ScriptService] // [System.Web.Script.Services.ScriptService] public class Tripadvisor : System.Web.Services.WebService { public Tripadvisor () { //Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string HotelAvailability(string api) { JavaScriptSerializer js = new JavaScriptSerializer(); […]

无法将当前JSON对象(例如{“name”:“value”})反序列化为类型’System.Collections.Generic.List`1

using System; using System.Collections.Generic; using System.Linq; using System.Text; using Facebook; using Newtonsoft.Json; namespace facebook { class Program { static void Main(string[] args) { var client = new FacebookClient(acc_ess); dynamic result = client.Get(“fql”, new { q = “select target_id,target_type from connection where source_id = me()”}); string jsonstring = JsonConvert.SerializeObject(result); //jsonstring {“data”:[{“target_id”:9503123,”target_type”:”user”}]} List datalist = JsonConvert.DeserializeObject<List>(jsonstring); } […]

C#.net如何反序列化JSON的复杂对象

我有一个JSON字符串,我需要一些帮助来反序列化它。 没有什么对我有用..这是JSON { “response”: [{ “loopa”: “81ED1A646S894309CA1746FD6B57E5BB46EC18D1FAff”, “drupa”: “D4492C3CCE7D6F839B2BASD2F08577F89A27B4ff”, “images”: [{ “report”: { “nemo”: “unknown” }, “status”: “rock”, “id”: “7e6ffe36e-8789e-4c235-87044-56378f08m30df”, “market”: 1 }, { “report”: { “nemo”: “unknown” }, “status”: “rock”, “id”: “e50e99df3-59563-45673-afj79e-e3f47504sb55e2”, “market”: 1 } ] }] } 我有一个类的例子,但我不必使用这些类,我不介意使用其他类。 这些是类: public class Report { public string nemo { get; set; } } public class […]

JSON.Net的“自检示循环检测”exception

我有一些代码将List对象发送到我的View(ASP.Net MVC): public ActionResult getRouteFromPart(int partId) { List routes = _routeService.GetRouteByPartType(partId); if (routes == null) { return this.AdvancedJsonResult(null, JsonRequestBehavior.AllowGet); } return this.AdvancedJsonResult(new { Routes = routes }, JsonRequestBehavior.AllowGet); } 但我在AdvancedJsonResult类中遇到exception: if (Data != null) { var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }; string result = JsonConvert.SerializeObject(this.Data, this.Formatting, settings); response.Write(result); } 我已经尝试了“ReferenceLoopHanding.Ignore”技巧,它使exception静音,但列表仍未传递给视图。 […]

Newtonsoft.JSON无法使用TypeConverter属性转换模型

我有一个C#MVC应用程序,它将数据作为JSON字符串存储在XML文档中,也存储在MySQL数据库表中。 最近我收到了在MySQL数据库字段中存储JSON字符串的要求,要通过Newtonsoft.Json转换为C#对象 ,所以我决定实现一个TypeConverter将JSON字符串转换为自定义C#模型。 不幸的是,当TypeConverter属性添加到我的C#Model时,我无法在我的解决方案中的任何地方使用以下命令来反序列化我的JSON字符串: JsonConvert.DeserializeObject(json); 删除属性可以解决问题但是这会阻止我将MySQL DB字段转换为自定义C#对象。 这是我添加了TypeConverter属性的C#模型 : using System.ComponentModel; [TypeConverter(typeof(FooConverter))] public class Foo { public bool a { get; set; } public bool b { get; set; } public bool c { get; set; } public Foo(){} } 这是我的TypeConverter类 : using Newtonsoft.Json; using System; using System.ComponentModel; public class FooConverter : TypeConverter { public […]

在streamingAssetsPath上读写文件

这是我在android中读取我的文本文件的方式。 #if UNITY_ANDROID string full_path = string.Format(“{0}/{1}”,Application.streamingAssetsPath, path_with_extention_under_streaming_assets_folder); // Android only use WWW to read file WWW reader = new WWW(full_path); while (!reader.isDone){} json = reader.text; // PK Debug 2017.12.11 Debug.Log(json); #endif 这就是我从pc上读取我的文本文件的方法。 #if UNITY_STANDALONE string full_path = string.Format(“{0}/{1}”, Application.streamingAssetsPath, path_with_extention_under_streaming_assets_folder); StreamReader reader = new StreamReader(full_path); json = reader.ReadToEnd().Trim(); reader.Close(); #endif 现在我的问题是我不知道如何在移动设备上编写文件因为我在独立版上这样做 #if UNITY_STANDALONE StreamWriter […]

将JSON文本加载到c#中的类对象中

如何将以下Json响应转换为C#对象? { “err_code”: “0”, “org”: “CGK”, “des”: “SIN”, “flight_date”: “20120719”, “schedule”: [ [“W2-888″,”20120719″,”20120719″,”1200″,”1600″,”03h00m”,”737-200″,”0″,[[“K”,”9″],[“F”,”9″],[“L”,”9″],[“M”,”9″],[“N”,”9″],[“P”,”9″],[“C”,”9″],[“O”,”9″]]], [“W2-999″,”20120719″,”20120719″,”1800″,”2000″,”01h00m”,”MD-83″,”0″,[[“K”,”9″],[“L”,”9″],[“M”,”9″],[“N”,”9″]]] ] }

Json转换空字符串而不是null

我正在尝试序列化我的结构,以便没有获得值的字符串获得其默认值“”而不是null [JsonProperty(PropertyName = “myProperty”, DefaultValueHandling = DefaultValueHandling.Populate)] [DefaultValue(“”)] public string MyProperty{ get; set; } 我在Json字符串中的结果: “myProperty”: null, 我想要的是 “myProperty”: “”, 我也试过创建一个没有任何影响的转换器,因为某些原因,转换和写入JJ函数甚至都没有触发: [JsonProperty(PropertyName = “myProperty”)] [JsonConverter(typeof(NullToEmptyStringConverter))] public string MyProperty{ get; set; } class NullToEmptyStringConverter : JsonConverter { public override bool CanConvert(Type objectType) { return objectType == typeof(object[]); } public override object ReadJson(JsonReader reader, Type objectType, object […]