Tag: json

如何返回JsonResult

我在编写这个AJAX方法时遇到了麻烦。 我想要返回一个JsonResult 。 (看起来像逻辑性的事情。)我能找到的所有例子都使用Json()将结果转换为JsonResult 。 [HttpPost] public JsonResult GetScoreResults(string userId, int chapterId) { return new Json(ChapterScoreResultsModel.Create(DbContext, userId, chapterId)); } 但是当我尝试这个时: 找不到类型或命名空间名称’Json’(您是否缺少using指令或程序集引用?) 但是,我无法在任何地方找到这个符号,我发现使用它的所有文章似乎都没有做任何特别的事情。 我确实在命名空间System.Web.Helpers找到了符号但是当我为它添加一个using ,我得到错误: 无法创建静态类’Json’的实例 从AJAX方法返回JsonResult的当前方法是什么?

反序列化递归JSON对象

我有一个表示查询表达式的递归JSON对象: { “where”: { “operator”: “AND”, “left”: { “operator”: “=”, “$fieldRef”: “requestor”, “value”: “@me” }, “right”: { “operator”: “=”, “$fieldRef”: “state”, “value”: “Closed” } } } 更多对象也可以出现在JSON层次结构中。 这是一个更复杂的例子: { “where”: { “operator”: “OR”, “left”: { “operator”: “=”, “$fieldRef”: “id”, “value”: “1234” }, “right”: { “operator”: “OR”, “left”: { “operator”: “=”, “$fieldRef”: “orgId”, “value”: “6757” }, […]

如何在Windows Phone中反序列化json数据?

最初我的json是格式的, “code”: 0, “message”: “success”, “students”: [ { “id”: “257633000000070001”, “name”: “hjeke”, “percentage”: 36, “type”: “Good”, }, { “id”: “257633000000073001”, “name”: “Second”, “percentage”: 4, “type”: “bad”, }] 所以我使用以下类使用Newtonsoft.json进行反序列化 [DataContract] public class students { [DataMember(Name = “code”)] public int Code { get; set; } [DataMember(Name = “message”)] public string Message { get; set; } [DataMember(Name = […]

将JSON中的数组反序列化为类/对象

我正在尝试将JSON数据存储到类中。 我可以将我的其他JSON字符串反序列化为类: var ser = JsonConvert.DeserializeObject(myJSON); 在我遇到arrays之前。 { \”Test\”: [{ \”FirstBool\”:1, \”aString\”:\”hello\” }] } 这是我的JSON类: public class Test { [JsonProperty(“FirstBool”)] public bool FirstBool { get; set; } [JsonProperty(“aString”)] public string aString { get; set; } } public class ResultObject { [JsonProperty(“Test”)] public List Test { get; set; } } 我如何反序列化我的非数组JSON : var ser = JsonConvert.DeserializeObject(myJSON); […]

在反序列化JSON String期间处理名称空间更改

我有2个应用程序在redis服务器的帮助下相互通信,在我的第一个应用程序中,我能够序列化和反序列化以及以下类型的对象 { “$type”: “System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib], [OPCMessagingService.Resource, OPCMessagingService]], mscorlib”, “71”: { “$type”: “OPCMessagingService.Resource, OPCMessagingService”, “SiteID”: 2, “ResourceID”: 71, “ProcessOrder”: “001000380873”, “CurrentStatus”: 0, “CycleTime”: 55, “DeviceList”: [ { “$type”: “OPCMessagingService.StackLight, OPCMessagingService”, “ResourceId”: 71, “DeviceIp”: “10.142.117.7”, “ComPort”: “COM14” }, { “$type”: “OPCMessagingService.LED, OPCMessagingService”, “ResourceId”: 71, “DeviceIp”: “10.142.117.3”, “ComPort”: “COM10”, “IsMessageDelivered”: false, “RetryCount”: 3, “IsRetryRequired”: true, “_messageHeader”: “:*HF601011471”, “_messageText”: […]

如何将json图像(byet数组)反序列化为asp.net中的图像?

我的json数组: {” lstFooddtlResult”:[{“class_name”:”Veg”,”food_name”:”pizza”,”food_photo”:[255,216,255,224,0,16,74,70,73,70,0,1……..],”food”:”12″,”qty_uom”:”Pcs”,”unit”:null}]} C#类: public class Food{ public string class_name { get; set; } public string food_name { get; set; } public photo[] photo { get; set; } public int food { get; set; } public string qty_uom { get; set; } public string unitcost { get; set; } } public class photo{ public byte […]

读取特定JSON字符串时出现对象引用错误?

我已经使用JSON到C#类转换器 ,它生成以下类: JSON {“ios_info”:{“serialNumber”:”F2LLMBNJFFF”,”imeiNumber”:”01388400413235″,”meid”:””,”iccID”:”8901410427640096045″,”firstUnbrickDate”:”11\/27\/13″,”lastUnbrickDate”:”11\/27\/13″,”unbricked”:”true”,”unlocked”:”false”,”productVersion”:”7.1.2″,”initialActivationPolicyID”:”23″,”initialActivationPolicyDetails”:”US AT&T Puerto Rico and US Virgin Islands Activation Policy”,”appliedActivationPolicyID”:”23″,”appliedActivationDetails”:”US AT&T Puerto Rico and US Virgin Islands Activation Policy”,”nextTetherPolicyID”:”23″,”nextTetherPolicyDetails”:”US AT&T Puerto Rico and US Virgin Islands Activation Policy”,”macAddress”:”ACFDEC6C988A”,”bluetoothMacAddress”:”AC:FD:EC:6C:98:8B”,”partDescription”:”IPHONE 5S SPACE GRAY 64GB-USA”},”fmi”:{“@attributes”:{“version”:”1″,”deviceCount”:”1″},”fmipLockStatusDevice”:{“@attributes”:{“serial”:”F2LLMBNJFFFQ”,”imei”:”013884004132355″,”isLocked”:”true”,”isLost”:”false”}}},”product_info”:{“serialNumber”:”F2LLMBNJFFF”,”warrantyStatus”:”Apple Limited Warranty”,”coverageEndDate”:”11\/25\/14″,”coverageStartDate”:”11\/26\/13″,”daysRemaining”:”497″,”estimatedPurchaseDate”:”11\/26\/13″,”purchaseCountry”:”United States”,”registrationDate”:”11\/26\/13″,”imageURL”:”http:\/\/service.info.apple.com\/parts\/service_parts\/na.gif”,”explodedViewURL”:”http:\/\/service.info.apple.com\/manuals-ssol.html”,”manualURL”:”http:\/\/service.info.apple.com\/manuals-ssol.html”,”productDescription”:”iPhone 5S”,”configDescription”:”IPHONE 5S GRAY 64GB GSM”,”slaGroupDescription”:””,”contractCoverageEndDate”:”11\/25\/15″,”contractCoverageStartDate”:”11\/26\/13″,”contractType”:”C1″,”laborCovered”:”Y”,”limitedWarranty”:”Y”,”partCovered”:”Y”,”notes”:”Covered by AppleCare+ – Incidents Available”,”acPlusFlag”:”Y”,”consumerLawInfo”:{“serviceType”:””,”popMandatory”:””,”allowedPartType”:””}}} 从JSON以上读取所有数据,但only the line at which i get […]

从序列化ViewModel还原时,Combox SelectedItem不适用

在恢复ViewModel(使用Json.Net序列化)时使用C#WPF和MVVM模式时,我遇到了一个奇怪的问题。 该软件的想法是 – 关闭窗口时 – 将当前Viewmodel状态保存在json文件中。 在下一次启动时,应用程序只是为json提供支持。 如果有文件,则将其反序列化并恢复ViewModel(设置公共属性)。 如果没有文件,则创建viewmodel并设置默认值。 现在我的问题是,当使用json文件恢复它时,包含自定义类型列表的combobox,combobox具有值但没有SelectedItem 。 在创建viewmodel实例并使用默认值初始化公共属性时(通过后面的代码执行此操作),一切都很好。 以下是一些代表“错误”的代码:View 代码背后 using System; using System.Windows; using System.IO; using Newtonsoft.Json; namespace CrazyWpf { public partial class MainWindow : Window { private DemoViewModel dvm; public MainWindow() { InitializeComponent(); this.dvm = (DemoViewModel)this.rootElement.DataContext; } private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), “settings.json”); […]

以适当的类型从JsonConvert.SerializeXNode返回json

var test = new { TestStr = “test”, TestNumber = 123, TestDate = new DateTime(1986, 1, 13, 17, 50, 31), TestBool = true }; var xml = JsonConvert.DeserializeXNode(JsonConvert.SerializeObject(test), “test”); 这段代码返回了很好的xml元素: 1986-01-13T14:50:31Z true 123 test 当我尝试将此xml转换回JSON时 : var json = JsonConvert.SerializeXNode(xml, Formatting.None, true); 我只使用String属性获取JSON。 我该怎么做才能让json得到合适的类型?

Json.net无法加载属于类对象的某些属性?

注意:我正在添加此社区wiki条目以节省某人我刚刚调试此问题所花费的时间。 我有一个具有多个公共属性的类对象。 我可以使用JSON.net将其序列化。 但是当我加载JSON文本并使用JsonConvert.DeserializeObject对其进行反序列化时,如果在序列化时它们肯定具有有效值,则某些字段将设置为NULL 。 我手动检查了序列化的JSON字符串,我肯定会在文本中看到NULL属性的值。 为什么会这样?