Tag: json

Json Value不分配给List

由于我是web api的新手,我发现将json List到Web API有些困难。 Json [ { “ItemId”:20, “RegId”:”VISIT0001778″, “BLoadDetailId”:”8/31/2018 12:28:10 PM”, “OrderReferenceNo”:null, “StartTime”:”0001-01-01T00:00:00″, “InvalidItemMsg”:””, “InvalidItemstatus”:false, “BLoadingBay”:”Chute 009″, “BLoadingBayCode”:null, “BLoadingBayID”:7, “RFID”:7123, “GangId”:2, “BOrderTransfer”:false, “BLoadedBags”:0.0, “BRemainingBags”:0.0, “BConversionValue”:null, “WHid”:2 } ] 课程: public class clsStartTimeUpdate { public int ItemId { get; set; } public string RegId { get; set; } public string BLoadDetailId { get; set; } […]

在c#中反序列化json数组列表

我正在开发一个主要以C#作为后端的项目,但我不是一个经验丰富的C#开发人员,所以我无法找到热点来修复对象列表的json反序列化。 以下函数负责反序列化,但是我收到错误: using System.IO; using System.Web; using Raven.Imports.Newtonsoft.Json; namespace Corina.Web.Handlers { public class JsonRequestHandler { public T Handle(HttpContextBase context) { string requestData; context.Request.InputStream.Position = 0; using (var inputStream = new StreamReader(context.Request.InputStream)) { requestData = inputStream.ReadToEnd(); } return JsonConvert.DeserializeObject(requestData, new Raven.Imports.Newtonsoft.Json.Converters.StringEnumConverter()); } } } Error : 无法将当前JSON数组(例如[1,2,3])反序列化为类型“Corina.Web.Views.DocumentViewModel”,因为该类型需要JSON对象(例如{“name”:“value”})才能正确反序列化。 要修复此错误,请将JSON更改为JSON对象(例如{“name”:“value”})或将反序列化类型更改为数组或实现集合接口的类型(例如ICollection,IList),例如List从JSON数组反序列化。 JsonArrayAttribute也可以添加到类型中以强制它从JSON数组反序列化。 任何人都可以告诉我如何在对象列表而不是对象上进行反序列化?

使用RestSharp客户端反序列化嵌套的JSON响应

我想使用REST Api并反序列化嵌套的JSON响应。 为此,我尝试创建一些代表JSON Response [1]的POCO类。 响应如下: { “success”: true, “message”: “OK”, “types”: [ { “name”: “A5EF3-ASR”, “title”: “ITIL Foundation Plus Cloud Introduction”, “classroomDeliveryMethod”: “Self-paced Virtual Class”, “descriptions”: { “EN”: { “description”: “some Text null”, “overview”: null, “abstract”: “Some other text”, “prerequisits”: null, “objective”: null, “topic”: null } }, “lastModified”: “2014-10-08T08:37:43Z”, “created”: “2014-04-28T11:23:12Z” }, { “name”: […]

如何使用JSON.net引用外部文件?

这是我在JSON.net尝试读取我的JSON模式时遇到的错误( return JsonSchema.Read(new JsonTextReader(reader)); ): 2014-07-15 15:33:42.7011 [Fatal] Newtonsoft.Json.JsonException: Could not resolve schema reference ‘data-result.json’. at Newtonsoft.Json.Schema.JsonSchemaBuilder.ResolveReferences(JsonSchema schema) in c:\Development\Releases\Json\Work ing\Newtonsoft.Json\Src\Newtonsoft.Json\Schema\JsonSchemaBuilder.cs:line 139 at Newtonsoft.Json.Schema.JsonSchemaBuilder.ResolveReferences(JsonSchema schema) in c:\Development\Releases\Json\Work ing\Newtonsoft.Json\Src\Newtonsoft.Json\Schema\JsonSchemaBuilder.cs:line 179 at Newtonsoft.Json.Schema.JsonSchemaBuilder.Read(JsonReader reader) in c:\Development\Releases\Json\Working\Newtonsof t.Json\Src\Newtonsoft.Json\Schema\JsonSchemaBuilder.cs:line 85 at Newtonsoft.Json.Schema.JsonSchema.Read(JsonReader reader, JsonSchemaResolver resolver) in c:\Development\Releases\ Json\Working\Newtonsoft.Json\Src\Newtonsoft.Json\Schema\JsonSchema.cs:line 280 at Newtonsoft.Json.Schema.JsonSchema.Read(JsonReader reader) in c:\Development\Releases\Json\Working\Newtonsoft.Json\ Src\Newtonsoft.Json\Schema\JsonSchema.cs:line 266 at ThinkBinary.SchemaToPoco.Core.JsonSchemaToCodeUnit.LoadSchema(String file) […]

DataContractJsonSerializer可以处理循环引用吗?

是否有DataContractSerializer 可以处理的序列化/反序列化方案,而DataContractJsonSerializer 不能 ? 特别是,我正在考虑循环引用: 这个MSDN页面解释了DataContractSerializer通过在DataContractAttribute构造函数中使用IsReference = true来管理循环引用。 另一方面, DataContractAttribute.IsReference文档没有明确声明其适用性仅限于DataContractSerializer 。 DataContractJsonSerializer也会尊重IsReference属性?

在C#GET请求中将JSON数据转换为查询字符串

将JSON对象转换为查询字符串以附加到GET Url的最佳方法是什么? POST很简单,我的Web API后端会读取它。 {姓名:’迈克’} =?姓名=迈克 private static string MakeRequest(HttpWebRequest req, string data) { try { if (req.Method == Verbs.POST.ToString() || req.Method == Verbs.PUT.ToString() || req.Method == Verbs.DELETE.ToString()) { var encodedData = Encoding.UTF8.GetBytes(data); req.ContentLength = encodedData.Length; req.ContentType = “application/json”; req.GetRequestStream().Write(encodedData, 0, encodedData.Length); } using (var response = req.GetResponse() as HttpWebResponse) using (var reader = new […]

.NET C#/ Csharp中的JSON – 从对象中获取值

我想解析一些JSON数据。 我正在使用James Newton-King的JSON.NET库 。 我将JSON字符串解析为JObject。 这是我正在解析的JSON: “root”: [ { “date”: 1325400000000, “id”: 12313131, “loc”: “en_us”, “name”: “New York, NY”, “products”: [ { “@type”: “asdf”, “city”: “New York – Penn Station, NY (NYP)”, “code”: “USA”, } ], “summary”: { “alert”: [], “end”: 1325577000000, “start”: 1325400000000 } } ] } 你可以看到它非常复杂。 “root”是必要的,因为其他人无法将数据解析为JObject实例。 JObject o = JObject.Parse(jsonString); JSON数据非常大。 […]

将nHibernate查询序列化为JSON

在深入研究Fluent nHibernate时,我发现了使用它的潜在破坏者…… 给出以下POCO代码。 public class Customer { public virtual int Id { get; set; } public virtual string Name { get; set; } public virtual Details Details { get; set; } } public class Details { public virtual int Id { get; set; } public virtual IList Orders { get; set; } } public class […]

在C#中如何编码XML以在页面的JavaScript部分中将其输出到JSON内部

我有一个上传KML文件的处理程序,并返回带有KML文件作为属性的JSON: context.Response.Write(“{\”name\”:\”” + FileName + “\”,\”type\”:\”” + FileType + “\”,\”size\”:\”” + FileSize + “\”,\”region_id\”:\”” + regionID + “\”,\”kml\”:\”” + HttpUtility.HtmlEncode(xmlData) + “\”}”); 正如您所看到的,我正在尝试使用HttpUtility.HtmlEncode对KML进行编码,但我的响应中出现错误: 未捕获的exception:无效的JSON 如何在C#中对XML / KML文件进行属性编码,以便稍后在JavaScript中对其进行解码? 编辑#1:根据Cheeso的评论我在IIS 7.5 Windows 7上使用ASP.NET,.NET版本4.我的处理程序是一个ashx文件。 如果我从响应中HttpUtility.HtmlEncode(xmlData) KML数据( HttpUtility.HtmlEncode(xmlData) ),则响应正常。 编辑#2我也尝试根据主持人的评论使用System.Web.Script.Serialization.JavaScriptSerializer 。 我这样使用它: System.Web.Script.Serialization.JavaScriptSerializer serializer; context.Response.Write(“{\”name\”:\”” + FileName + “\”,\”type\”:\”” + FileType + “\”,\”size\”:\”” + FileSize + “\”,\”region_id\”:\”” + regionID […]

如何使用嵌套的对象数组反序列化JSON数组

我有一个JSON字符串,如下所示,并希望反序列化它: [[{“campaignId”:201410018,”programCode”:”54321″}],[{“campaignId”:201410018,”programCode”:”54321″}]] 我创建了一些类,如下所示: public class Rootclass { public List rootClass { get; set; } } public class JSONResponse { public int campaignId { get; set; } public string programCode { get; set; } } 我正在调用此JSON.NET方法来反序列化JSON: List myDeserializedObjList = (List)Newtonsoft.Json.JsonConvert.DeserializeObject(json, typeof(List)); 但我收到以下错误: Cannot deserialize JSON array (ie [1,2,3]) into type ‘JSON_Test.Rootclass’. The deserialized type must be […]