Tag: json

无法在C#中反序列化JSON结果。 输入字符串格式错误不正确

我试图将json输出反序列化为C#对象。 JSON结果: {“order”:{“commission”:3.490000,”cost”:4.490000,”duration”:”day”,”extended_hours “:false,”fees”:0.000000,”class”:”equity”,”price”:1.000000,”quantity”:1.000000,”r equest_date”:”2013-11-26T09:43:17.118Z”,”result”:true,”side”:”buy”,”status”:”ok” ,”symbol”:”DIS”,”type”:”limit”}} 我从JSON派生的类: public class Rootobject { public Order Order { get; set; } } public class Order { public float commission { get; set; } public float cost { get; set; } public string duration { get; set; } public bool extended_hours { get; set; } public int fees { […]

在Web Api控制器中将JSON反序列化为字典

我有这样的JSON字符串: ‘{“1″:[1,3,5],”2″:[2,5,6],”3”:[5,6,8]}’ 我想将其发送到Web Api控制器而不使用ajax请求更改: $.ajax({ type: “POST”, url: “Api/Serialize/Dict”, data: JSON.stringify(sendedData), dataType: “json” }); 在Web Api我有这样的方法: [HttpPost] public object Dict(Dictionary<int, List> sendedData) { //code goes here return null; } 总是sendedData == null. 换句话说:我不知道如何将JSON反序列化为(Dictionary<int, List> 。 谢谢你的答案。

C#使用newtonsoft删除json子节点

我正在使用.net 3.5中的c#wpf开发一个应用程序。 我使用newtonsoft库来解析json字符串。 我想知道如何删除json的子节点。 例如,我的json数据= {“employees”:[ {“firstName”:”John”, “lastName”:”Doe”}, {“firstName”:”Anna”, “lastName”:”Smith”}, {“firstName”:”Peter”, “lastName”:”Jones”}]} function jobject.Remove(“employees”); 成功删除所有节点 我想知道如何单独删除第一个员工细节。

web.config maxJsonLength问题

我尝试从一侧使用HttpClient.PostAsJsonAsync发送大消息json(带图像),并尝试使用Microsoft.AspNet.Mvc version =“5.2.3”在Controller中获取数据。 当我发送小消息时,一切都很好。 移动代码详情: private async Task Method(string url, TRequest request, IEnumerable<KeyValuePair> headers) { using (var client = new HttpClient(_httpClientHandlerFactory.CreateHandler())) { client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(“application/json”)); client.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue { NoCache = true }; AddHeadersToClient(client, headers); var response = await client.PostAsJsonAsync(url, request); … 另一方面: public class MyController: Controller { [HttpPost] public ActionResult EmailBody(string siteShortName, string […]

Newtonsoft Json Deserlize作为C#Datagridview

我在使用Newtonsoft Json插件时遇到了一些问题。 我想用Json填充datagridview但不知道如何。 在Newtonsoft Json的文档中,我得到了一个数据表的例子,但如果我尝试这个样本,我只会得到错误。 这是我的Json: [ { “id”: “17”, “name”: “Filename”, “author”: “unknown”, “size”: “3.1MB”, “pfad”: “ftp://path/Filename”, “Filetoken”: “6747rzuzur6urzut766754677” }, { “id”: “20”, “name”: “Filename”, “author”: “unknown”, “size”: “3.1MB”, “pfad”: “ftp://path/Filename”, “Filetoken”: “6747rzuzur6urzut766754677” } ] 我试着用这个例子和这个 也许有人可以帮忙吗?

循环引用和ScriptIgnore问题

我有几个相互引用的BusinessObject类,我需要在JsonResponse中序列化一个并将其返回到我的视图中。 我一直得到循环引用exception,我无法摆脱它。 我已将[ScriptIgnore()]装饰器放在每个不是简单数据类型属性的属性上,但我仍然得到exception。 我无法弄清楚问题出在哪里,因为我阻止了序列化器几乎所有的东西,它仍然在炸毁我。 有没有办法看到它们当前状态的序列化对象是什么? [HttpPost] public JsonResult GetAnalysisInfo(int id) { ProjectContext myDB = db; SearchAnalysis searchanalysis = SearchAnalysis.Find(myDB, id); //searchanalysis.Results = searchanalysis.SearchResultsPrototype(myDB); return this.Json(searchanalysis); } 更新 我也试过实现ISerializable无济于事。 我的GetObjectData非常简单: public void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue(“SearchAnalysisId”, this.SearchAnalysisId); info.AddValue(“Created”, this.Created); } 仍然收到CircularRefernce错误。 DateTime数据类型不会导致序列化问题吗?

使用JSON.NET部分反序列化,保留一些原始字段

我有这样的文件 { “Field1”: 1, “Field2”: 2, “Field3”: { Type: “TheMotherLoad” } } 我想转换成这个类,但保持字段3“原始/原样”。 public class Fields { public int Field1 { get; set; } public int Field2 { get; set; } public string Field3 { get; set; } } 结果应该是 Field1 = 1, Field2 = 2, Field3 = “{ Type: “TheMotherLoad” }” 可以用Json.NET吗?

使用Newtonsoft Json从流中反序列化多个json对象

我正在为json字符串读取NetworkStream ,然后使用Newtonsoft.Json对其进行反序列 Newtonsoft.Json 。 有时,两个json对象可以在流上同时发送回来并同时读取。 但是Newtonsoft.Json serializer只给了我一个对象。 例如,如果我在流上有以下字符串: {“name”:”John Doe”,”age”:10}{“name”:”Jane Doe”,”age”:10} 如果我反serializer流, serializer将读取整个流,但只提供第一个对象。 有没有办法让serializer只读取流中的第一个对象,然后在循环的下一次迭代中读取下一个对象? 码: public static Person Deserialize(Stream stream) { var Serializer = new JsonSerializer(); var streamReader = new StreamReader(stream, new UTF8Encoding()); return Serializer.Deserialize(new JsonTextReader(streamReader)); } 我无法将其序列化为列表,因为我没有收到json数组。

当数组表示为具有“item#XXX”属性的对象时,请读取JSON

我有这个JSON文本,我无法弄清楚如何解析“items”属性来填充项目列表 { “response”: { “success”: 1, “current_time”: 1445015502, “items”: { “item1”: { “property1”: 1, “property2”: “test”, “property3”: 4.3 }, “item2”: { “property1”: 5, “property2”: “test2”, “property3”: 7.8 } } } } 这些是我的课程: public class Item { public int property1 { get; set; } public string property2 { get; set; } public double property3 { get; […]

jquery ajax’post’来电

我是jQuery和Ajax的新手,我遇到了’post’的问题。 我正在使用jQuery Ajax“post”调用将数据保存到数据库。 当我尝试保存数据时,它将null传递给我的C#方法。 jQuery看起来像这样: function saveInfo(id) { var userID = id; var userEmail = $(‘.userEmail’).val(); var userName = $(‘.userName’).val(); var dataJSON = {“userID”: userID, “userEmail”: userEmail, “userName”: userName}; $.ajax({ type: ‘POST’, url: ‘../../Services/AjaxServices.svc/SaveUser’, data:JSON.stringify(dataJSON), contentType: ‘application/json; charset=utf-8’, dataType: ‘json’ }); return false; }` .userEmail和.userName是对输入字段的类引用。 C#代码如下所示: [ServiceContract(Namespace = “http://testUsePage.com”)] [AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)] public class AjaxServices { [OperationContract] […]