Tag: json.net

C#JsonConvert.DeserializeAnonymousType失败

我试图反序列化Azurefunction应用程序中的字符串输入。 我的意见是 [{“messageid”:1, “deviceid”:”Android”, “temperature”:20.0, “humidity”:47.0, “eventprocessedutctime”:”2017-12-01T10:35:57.8331048Z”, “result1”:{“temperature”:”20″,”humidity”:”47″,”Scored Labels”:”NO”,”Scored Probabilities”:”0.450145334005356″}}] 我尝试使用此代码运行。 #r “Newtonsoft.Json” using System.Configuration; using System.Text; using System.Net; using Microsoft.Azure.Devices; using Newtonsoft.Json; // create proxy static Microsoft.Azure.Devices.ServiceClient client = ServiceClient.CreateFromConnectionString(ConfigurationManager.AppSettings[“myIoTHub”]); public static async Task Run(string input, HttpRequestMessage req, TraceWriter log) { log.Info($”ASA Job: {input}”); var data = JsonConvert.DeserializeAnonymousType(input, new { deviceid = “” }); […]

将两个值反序列化为同一属性

我有一个客户端可以调用两个不同版本的服务。 一项服务只发送一个值: { “value” : { … } } 第二个服务总是返回多个值: { “values” : [ { … }, { … } ] } 理想情况下,我想在客户端类中使用单个对象来表示这一点,因此用户永远不会看到它是单个值还是多个值。 public class MyValues { public List Values { get; set; } public Thing Other { get; set; } } 我认为我能够实现这一目标的唯一方法是使用我应用于MyValues的自定义JsonConverter类,但我真的只想在我反序列化属性value时做一些自定义操作。 我似乎无法弄清楚IContractResolver是否是更好的方法(例如以某种方式将幻像属性附加到MyValues,反序列化value并将其放入Values 。 如果我创建一个自定义转换器,如何告诉它通常反序列化其他所有内容(例如,如果Other具有额外的属性,请确保它们被正确处理,等等)

Json.net捕获重复项并抛出错误

可能重复: Json.NET(Newtonsoft.Json) – 两个同名的“属性”? 我正在使用JSON.NET将JSON文件反序列化为字典。 现在我想做的是有以下几行: JsonConvert.DeserializeObject<IDictionary>(text); 如果JSON中有重复条目,则抛出exception,如下所示: { “ExampleText”: “Example 1”, “ExampleText”: “Example 2”, “ExampleText”: “Example 3”, } 标准的JSON.NET行为只是将“ExampleText”条目替换为JSON中最后一个条目。 是否可以抛出exception?

使用JsonConverter反序列化JSON字典值

我使用Json.NET库来反序列化JSON。 对于抽象类Foo我有一个自定义的JsonConverter 。 这是我使用它的方式: [JsonConverter(typeof(FooJsonConverter))] public Foo MyFoo { get; set; } 到现在为止还挺好。 当我在Dictionary中使用Foo类时会出现问题。 这是我的尝试: [JsonDictionary(ItemConverterType = typeof(FooJsonConverter))] public Dictionary MyFooDictionary { get; set; } 但上面给出了错误: 属性“JsonDictionary”在此声明类型上无效。 它仅对“类,接口”声明有效。 如何为Dictionary值指定转换器?

在JSON.NET中解析JSON数组

我在REST API响应中有JSON对象: { Result: [ { “id”: 1, “id_endpoint”: 1, “name”: “Endpoint 1”, “description”: “Endpoint 1”, “unit”: “mmol”, “minthreshold”: 30, “maxthreshold”: -15, “id_device”: 4, “value”: 7, “time”: “2016-12-24T21:20:19.000Z”, “address”: “Endpoint 1”, “id_user”: 1 }, { “id”: 2, “id_endpoint”: 1, “name”: “Endpoint 1”, “description”: “Endpoint 1”, “unit”: “mmol”, “minthreshold”: 30, “maxthreshold”: -15, “id_device”: 4, “value”: 6, […]

是否存在用于JsonTextReader的XmlTextReader.ReadContentAsBase64模拟

我有一个包含二进制属性的JSON,其值非常大: { “NormalStringValue” : “test”, “NormalIntValue” : 1, “LargeBinaryValueInBase64”: “AQB….” } 标准的Newtonsoft JsonTextReader.ReadAsBytes读取JsonTextReader.ReadAsBytes的值,因此分配了一个大字节数组。 我想避免这个。 对于XmlTextReader有一个ReadContentAsBase64(byte[] buffer, int offset, int count)方法,可以执行BASE64内容的缓冲读取。 是否有任何库为JSON数据实现这样的方法? 或者也许可以通过Json.NET实现?

将JsonDictionary属性应用于字典

如果我尝试将JsonDictionary属性添加到.net Dictionary(Of Integer,MyClass),编译器告诉我,该属性无法应用。 为什么是这样? Public ReadOnly Property monatswerte As Dictionary(Of Integer, MyClass) 我基本上找不到任何关于如何在线使用JsonDictionary的例子。

使用Json.Net反序列化时,在运行时将System.Type用作

我有一个进程需要能够以下面的方式调用函数。 public static Task Convert(string payload, Type type) { JsonSerializerSettings settings = new JsonSerializerSettings().Configure();//<— Pull in extension methods to handle custom types return JsonConvert.SerializeObject(JsonConvert.DeserializeObject(payload), settings)); } 上面的代码不能编译。 接下来,我使用John Skeet对2011年类似于我的问题的回答 。 public static string Convert(string payload, Type type) { JsonSerializerSettings settings = new JsonSerializerSettings().Configure(); //This clashes with another overload of DeserializeObject in the method table MethodInfo […]

C#反序列化派生并具有引用的对象

我有一个类型节点的对象。 Node.cs 我打电话时序列化工作如下: var nodeSer = JsonConvert.SerializeObject(mynode, Formatting.Indented, new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects }); 我的问题是以下调用不起作用。 var n = JsonConvert.DeserializeObject(nodeSer, new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects, TypeNameHandling = TypeNameHandling.Auto }); 该调用导致以下错误: Newtonsoft.Json.JsonSerializationException: “ISerializable type ‘System.Action’ does not have a valid constructor. To correctly implement ISerializable a constructor that takes SerializationInfo and StreamingContext parameters should be […]

将字段添加到JSON数组中

我有一个带有这样的数组的JSON字符串: { “Id”: 123, “Username”: “Sr. X”, “Packages”: [ { “Name”: “Cups”, “SupplierId”: 1, “ProviderGroupId”: 575, “SupplierName”: “Foo Cups” }, { “Name”: “Pins”, “SupplierId”: 5, “ProviderGroupId”: 1082, “SupplierName”: “Foo Pins” } ] } 我想在Packages数组中添加一个新字段,如: “Packages”: [ { “Name”: “Cups”, “SupplierId”: 1, “ProviderGroupId”: 575, “SupplierName”: “Foo Cups”, “New Field”: “Value” },… 现在我可以添加一个新字段但是在主对象中,我正在使用Json.NET库来完成这项工作,但似乎文档没有达到那个级别。 有没有人以前做过?