Tag: json

如果内容长度> 7kb使用C#,则无法在WebRequest上发布

我正在尝试将Json发布到我们的Web服务。 我的问题是,当我的request.ContentLength超过7kb时。 我将在request.GetResponse()处获得Webexception500。 但是如果我的request.ContentLength低于7kb,我的post就成功发送了。 错误信息: The remote server returned an error: (500) Internal Server Error. 源代码: public static string JsonPost(string url, string method, string postData) { Uri address = new Uri(url + method); HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; request.Method = “POST”; request.ContentType = “application/json”; byte[] byteData = UTF8Encoding.UTF8.GetBytes(postData); request.ContentLength = byteData.Length; using (Stream postStream […]

.NET / WPF中的通用JSON解析器?

我已经阅读了很多关于如何使用DataContractJsonSerializer将JSON对象反序列化为特定对象的教程。 但是,我想将我的对象反序列化为由字符串,数组或字典组成的字典,例如当我说JsonObject.Parse(myJSONstring)时System.Json与SilverLight一起使用。 是否可以在我的WPF项目中使用System.Json的等价物? (只是一个简短的背景:我正在获取有很多信息的JSON对象,我只是想用一点来填充一个String数组) 干杯 聂

如何向/与RESTful WCF服务传递和使用JSON参数?

我是RESTful服务的初学者。 我需要创建一个接口,客户端需要传递最多9个参数。 我宁愿将参数作为JSON对象传递。 例如,如果我的JSON是: ‘{ “age”:100, “name”:”foo”, “messages”:[“msg 1″,”msg 2″,”msg 3”], “favoriteColor” : “blue”, “petName” : “Godzilla”, “IQ” : “QuiteLow” }’ 如果我需要在下面执行下面的服务器端方法: public Person FindPerson(Peron lookUpPerson) { Person found = null; // Implementation that finds the Person and sets ‘found’ return found; } 问题(S): 我应该如何使用上面的JSON字符串从客户端进行调用? 我如何创建RESTful服务方法的签名和实现 接受这个JSON, 将它解析并反序列化为Person对象和 调用/将FindPerson方法的返回值返回给客户端?

asp5 IConfigurationRoot获取json数组

我正在尝试向我的asp 5 mvc 6 web应用程序(“rc1-final”库)提供一些额外的配置数据,以便部署在Azure上。 此附加数据的一部分由json文件中的数据数组组成。 我可以将这个json文件添加到我的配置中,并且在调试时如果我观察配置对象,我可以浏览到提供程序条目并看到它确实联系了我正在寻找的数据数组。 但是,尝试Configuration[“extraData”]产生null(因为该属性的“值”为null,即使它显然包含元素数组。 我没有问题访问存储在json配置中的单个值(例如我的数据库连接字符串和其他值。)看起来好像不支持数组格式的json数据? 请参阅以下示例json文件(根据jsonlint进行validation,对我来说似乎很好)… //extraData.json { “ExtraData”: [ { “Id”: 1, “Description”: “The first identifier” }, { “Id”: 2, “Description”: “The second identifier” }, { “Id”: 3, “Description”: “The third identifier” } ] } …以及以下示例Startup.cs … //Startup.cs using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Hosting; […]

如何将json添加到RestSharp POST请求中

我有以下JSON字符串作为字符串参数传递到我的c#代码 – AddLocation(string locationJSON): {“accountId”:”57abb4d6aad4″,”address”:{“city”:”TEST”,”country”:”TEST”,”postalCode”:”TEST”,”state”:”TEST”,”street”:”TEST”},”alternateEmails”:[{“email”:”TEST”}],”alternatePhoneNumbers”:[{“phoneNumber”:”TEST”}],”alternateWebsites”:[{“website”:”TEST”}],”auditOnly”:false,”busName”:”593163b7-a465-43ea-b8fb-e5b967d9690c”,”email”:”TEST EMAIL”,”primaryKeyword”:”TEST”,”primaryPhone”:”TEST”,”rankingKeywords”:[{“keyword”:”TEST”,”localArea”:”TEST”}],”resellerLocationId”:”5461caf7-f52f-4c2b-9089-2ir8hgdy62″,”website”:”TEST”} 我正在尝试将JSON添加到像这样的RestSharp POST请求但它不起作用: public string AddLocation(string locationJSON) { var client = new RestClient(_authorizationDataProvider.LocationURL); var request = new RestRequest(Method.POST); request.RequestFormat = DataFormat.Json; request.AddHeader(“cache-control”, “no-cache”); request.AddHeader(“Authorization”, _authorizationResponse.Token); … request.AddJsonBody(locationJSON); var response = client.Execute(request); } 响应回复为“错误请求”。 如果我在调试器中检查响应,这是我得到的: {“code”:”invalid_json”,”details”:{“obj.address”:[{“msg”:[“error.path.missing”],”args”:[]}],”obj.rankingKeywords”:[{“msg”:[“error.path.missing”],”args”:[]}],”obj.alternatePhoneNumbers”:[{“msg”:[“error.path.missing”],”args”:[]}],”obj.busName”:[{“msg”:[“error.path.missing”],”args”:[]}],”obj.accountId”:[{“msg”:[“error.path.missing”],”args”:[]}],”obj.alternateEmails”:[{“msg”:[“error.path.missing”],”args”:[]}],”obj.alternateWebsites”:[{“msg”:[“error.path.missing”],”args”:[]}],”obj.email”:[{“msg”:[“error.path.missing”],”args”:[]}],”obj.primaryKeyword”:[{“msg”:[“error.path.missing”],”args”:[]}],”obj.auditOnly”:[{“msg”:[“error.path.missing”],”args”:[]}]}} 我在调用AddJsonBody之后检查了请求参数,并且值似乎包括双引号的转义序列 – 这似乎是个问题。 {\”accountId\”:\”57abb4d6aad4def3d213c25d\”,\”address\”:{\”city\”:\”TEST\”,\”country\”:\”TEST\”,\”postalCode\”:\”TEST\”,\”state\”:\”TEST\”,\”street\”:\”TEST\”},\”alternateEmails\”:[{\”email\”:\”TEST\”}],\”alternatePhoneNumbers\”:[{\”phoneNumber\”:\”TEST\”}],\”alternateWebsites\”:[{\”website\”:\”TEST\”}],\”auditOnly\”:false,\”busName\”:\”84e7ef98-7a9f-4805-ab45-e852a4b078d8\”,\”email\”:\”TEST EMAIL\”,\”primaryKeyword\”:\”TEST\”,\”primaryPhone\”:\”TEST\”,\”rankingKeywords\”:[{\”keyword\”:\”TEST\”,\”localArea\”:\”TEST\”}],\”resellerLocationId\”:\”06b528a9-22a6-4853-8148-805c9cb46941\”,\”website\”:\”TEST\”} 所以我的问题是如何将json字符串添加到请求主体?

Javascript DATE和C#date – 什么是最佳解决方案?

我使用以下代码从服务器端C#获取日期: DateTime d1 = new DateTime(1970, 1, 1); DateTime d2 = (DateTime)c.ccdTimestamp2; long x = new TimeSpan(d2.Ticks – d1.Ticks).TotalMilliseconds; 当我在javascript端获取代码时: function (timestamp) { alert(“testing :” + new Date(timestamp)) } 这给了我一个完全格式化的日期,但它没有带来我的时区的时间,因为如果它是17.15这里,它为我提供19.15 GMT +2! 起初我只是尝试传递我的c#时间戳,没有上面的任何代码,并发现了这个问题: 如何格式化Microsoft JSON日期? 但我不知道JSON是什么,我无法得到我能做的! 使用JSON更容易吗? 如果是这样,任何人都能指导我 非常感谢你 编辑:解决方案 – 我没有在服务器端使用通用时间。 我保留了服务器端代码。 我所做的就是: new Date(timestamp).toUTCString()

如何在ClassLibrary中引用JSON序列化?

我需要引用什么库来使用System.Web.Script.Serialization中的System.Web.Script.Serialization ? 已经使用System.Web.Script.Serialization引用了System.Web.Script.Serialization ; 仍然失败了。 目标框架是4.0。 在WebApplication项目中,一切都很好。

webapi2中DataMember和JsonProperty之间的区别

在webapi2中使用DataMember和JsonProperty有什么区别? 任何性能差异? 什么是首选使用? 谢谢! 安德烈亚斯

如何解析MultipartFormDataContent

我正在编写一个Web API服务,我想接受一个文件(图像)和一个包含图像关键信息的序列化对象(JSON)。 没有图像部分的问题但是当我添加包含反序列化对象的字符串内容时,我在尝试确定哪个是哪个并且相应地采取行动时遇到问题。 客户端代码如下所示: HttpClient client = new HttpClient(); MultipartFormDataContent content = new MultipartFormDataContent(); content.Add(new StreamContent(File.Open(“c:\\MyImages\\Image00.jpg”, FileMode.Open)), “image_file”, “Image00.jpg”); ImageKeys ik = new ImageKeys { ImageId = “12345”, Timestamp = DateTime.Now.ToString() }; JavaScriptSerializer js = new JavaScriptSerializer(); if (ik != null) { content.Add(new StringContent(js.Serialize(ik), Encoding.UTF8, “application/json”), “image_keys”); } string uri = “http://localhost/MyAPI/api/MyQuery/TransferFile”; var request = […]

json.net需要在json中找不到的属性

我正在使用Json.net,我得到了一个类如下 public class RecordAlias { [JsonProperty(PropertyName = “eId”, Required = Required.Always)] public string EntityId { get; set; } [JsonProperty(PropertyName = “aId”, Required = Required.AllowNull)] public string AliasId { get; set; } [JsonProperty(PropertyName = “iSd”, Required = Required.AllowNull)] public bool IsSelected { get; set; } } 因此,即使通过某些项目在json字符串中没有属性“iSd”,也可以对json进行反序列化,我希望如果不存在,则应该填充该类型的默认值,例如,IsSelected应该为false,除了最后一项 [{ “eId” : “30022004”, “aId” : “1” }, { […]