Tag: json

从类中获取要在查询字符串中使用的JSON属性名称列表

如果我有一个CSON模型类,JSON.net使用它来绑定序列化JSON字符串中的数据,有没有办法可以从该类创建一个查询字符串以便发出初始请求? 模型类示例: public class model { [JsonProperty(PropertyName = “id”)] public long ID { get; set; } [JsonProperty(PropertyName = “some_string”)] public string SomeString {get; set;} } 查询字符串示例: baseUrl + uri + “&fields=id,some_string” + token 所以我想要做的就是从模型对象中收集“id”和“some_string”,这样我就可以动态地创建一个“&fields”参数。 谢谢!

使用JSON.Net编写属性名称

我正在使用JSON.net在C#中编写一些json。 我可以像这样生成JSON { “id”: “234”, “name”: “abc” } 我想做的就是得到这个 { “DATA”: { “id”: “234”, “name”: “abc” } } 这是我正在使用的json.net代码 StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); JsonWriter jsonWriter = new JsonTextWriter(sw); jsonWriter.Formatting = Formatting.Indented; jsonWriter.WriteStartObject(); jsonWriter.WritePropertyName(“id”); jsonWriter.WriteValue(“234”); jsonWriter.WritePropertyName(“name”); jsonWriter.WriteValue(“abc”); jsonWriter.WriteEndObject(); 你能建议如何添加’DATA’部分吗?

C#无法将System.String转换或转换为Class对象

我试图反序列化从Web API收到的JSON字符串 try { string r = await App.client.GetUser(); App.Authentication = JsonConvert.DeserializeObject(r); await DisplayAlert(“TEST”, App.Authentication.ToString(), “OK”); Application.Current.MainPage = new Schedule(); } catch (Exception p) { await DisplayAlert(“Getting Authentication failed”, p.ToString(), “TEST”); } 但是它给出了错误:无法将System.String转换或转换为App1.ApiResult App.Authentication = JsonConvert.DeserializeObject(r); App.Authentication: public static ApiResult Authentication = new ApiResult();` JSON字符串: “\”{\\ “状态\\”:\\ “0 \\”,\\ “消息\\”:{\\ “ID \\”:5,\\ “姓\\”:\\”约翰\\”,\\ “名字\\”:\\ “李四\\”,\\ […]

无法从程序集加载类型System.Runtime.Versioning.TargetFrameWorkAttribute

我正在尝试编译一个使用库Newtonsoft.Json.dll和mono的程序 编译命令 gmcs Program.cs etcetera.cs -r:Newtonsoft.Json.dll -r:Argotic.Core.dll 结果: Missing method .ctor in assembly Newtonsoft.Json.dll, type System.Runtime.Versioning.TargetFrameworkAttribute Can’t find custom attr constructor image: Newtonsoft.Json.dll mtoken: 0x0a000053 然后当尝试运行程序( mono Program.exe )时,它会抛出错误: Unhandled Exception: System.TypeLoadException: Could not load type ‘System.Runtime.Versioning.TargetFrameworkAttribute’ from assembly ‘Newtonsoft.Json’. at my_program.CJSONStuff.serialize (System.Collections.Generic.Dictionary`2 obj) [0x00000] in :0 at my_program.TheObjDB.getAllSerialized () [0x00000] in :0 at my_program.Program.Main […]

c#使json无法在视图中正确呈现

嗨我试图发送一个字符串到一个看起来像json的视图。 我发送一份名单: class Place { public string title { get; set; } public string description { get; set; } public double latitude { get; set; } public double longitude { get; set; } } List placeList = new List(); //add places to PlaceList //Then i do this System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer(); string sJSON = […]

如何检查两个JSON对象是否相等?

我试图发现两个JSON字符串是否相等。 这是我之前尝试过的 var obj1 = Json.Decode(“{\”ValueA\”:1,\”ValueB\”:2}”) var obj2 = Json.Decode(“{\”ValueB\”:2,\”ValueA\”:1}”) // But then there seems to be no way to compare the two objects? 当然,必须存在一种优雅简单的方式来实现我认为的常见任务?

如何在.NET中自定义JSON枚举的反序列化?

我有以下示例C#代码,使用svcutils.exe应用程序从xsd自动生成。 [DataContract] public enum Foo { [EnumMember(Value = “bar”)] Bar = 1, [EnumMember(Value = “baz”)] Baz = 2 } [DataContract] public class UNameIt { [DataMember(Name = “id”)] public long Id { get; private set; } [DataMember(Name = “name”)] public string Name { get; private set; } [DataMember(Name = “foo”)] public Foo Foo { get; private […]

如何在代码中编写JSON字符串值?

我想将以下字符串存储在String变量中 { “ID”: “123”, “DateOfRegistration”: “2012-10-21T00:00:00 + 05:30”, “状态”:0} 这是我使用的代码.. String str=”{“Id”:”123″,”DateOfRegistration”:”2012-10-21T00:00:00+05:30″,”Status”:0}”; ..但它显示错误..

尝试反序列化从Exceptioninheritance的类时,Json.net失败

我有一个inheritance自Exception的类SearchError ,当我尝试从有效的json反序列化时,我得到以下exception: ISerializable type ‘SearchError’ does not have a valid constructor. To correctly implement ISerializable a constructor that takes SerializationInfo and StreamingContext parameters should be present. Path ”, line 1, position 81. 我尝试实现建议的缺少构造函数,但没有帮助。 这是实现建议的构造函数后的类: public class APIError : Exception { [JsonProperty(“error”)] public string Error { get; set; } [JsonProperty(“@http_status_code”)] public int HttpStatusCode { get; set; […]

Json序列化字典的问题

每当我尝试序列化字典时,我都会得到exception: System.ArgumentException: Type ‘System.Collections.Generic.Dictionary`2[[Foo.DictionarySerializationTest+TestEnum, Foo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]’ is not supported for serialization/deserialization of a dictionary, keys must be strings or object 我的测试用例是: public class DictionarySerializationTest { public enum TestEnum { A, B, C } //tried with numbers, too: public enum TestEnum { A = 1, B = 2, C = […]