Tag: json

如何干净地反序列化JSON,其中字符串值包装在同名对象中

我想将一些奇怪的JSON反序列化为C#类: { “Result”: { “Client”: { “ProductList”: { “Product”: [ { “Name”: { “Name”: “Car polish” } } ] }, “Name”: { “Name”: “Mr. Clouseau” }, “AddressLine1”: { “AddressLine1”: “Hightstreet 13” } } } } json2csharp为JSON生成以下类: public class Name { public string Name { get; set; } } public class Product { public Name Name […]

将JSON字符串解析为List

string json = “{\”People\”:[{\”FirstName\”:\”Hans\”,\”LastName\”:\”Olo\”} {\”FirstName\”:\”Jimmy\”,\”LastName\”:\”Crackedcorn\”}]}”; var obj = JObject.Parse(json); List first; List last; foreach (var child in obj[“People”].Children()) { var name = child.First()[“countryName”].ToString(); var two = child.First()[“countryCode”].ToString(); var three = child.First()[“isoAlpha3”].ToString(); countries.Add(name); twoCharCodes.Add(two); threeCharCodes.Add(three); Console.Write(“Name:\t\t{0}\n2CharCode:\t{1}\n3CharCode:\t{2}\n\n”, name, two, three); } 我正在寻找一种方法将每个FirstName值添加到第一个List中,并将LastName值与最后一个List相同。 这样做的最佳方法是什么? 上面的代码打破了: var name = child.First()[“countryName”].ToString(); 有这个错误: Cannot access child value on Newtonsoft.Json.Linq.JProperty 任何建议?

将C#对象转换为Json对象

我试图将C#对象序列化为Json对象。 然后将其提交给Salesforce API,并创建一个应用程序。 现在我将C#对象序列化为Json字符串,但我需要它作为一个对象。 这是我的C#对象以及伴随序列化。 Customer application = new Customer { ProductDescription = “gors_descr ” + tbDescription.Text, Fname = “b_name_first ” + tbFName.Text, Lname = “b_name_last ” + tbLName.Text }; var json = new System.Web.Script.Serialization.JavaScriptSerializer(); string jsonString = json.Serialize(application); string endPoint = token.instance_url + “/services/apexrest/submitApplication/”; string response = conn.HttpPost(endPoint, json, token); Literal rLiteral = this.FindControl(“resultLiteral”) […]

使用非法变量字符在C#中反序列化json

我正在编写一个与REST服务交互的.NET库(使用Newtonsoft),我有一个返回json的服务,但json id字段被称为’$ id’。 所以我不能在我的C#数据类中创建相应的$ id属性。 我试着用类似的东西 [JsonObject(MemberSerialization.OptOut)] public class DocData { [JsonProperty(“$id”)] public string id { get; set; } public string Name { get; set; } } 但是当Name被分配时,id属性不会。 有谁知道如何将这个json密钥映射到.NET? 谢谢

使用webapi和引导日期选择器在angularjs中绑定日期

给定一个返回json值的WebApi2服务,如下所示: { id: 1109, effectiveDate: “2014-10-05T00:00:00”, // the date is a string (newtonsoft.json) text: “Duis et rhoncus nibh. Cras rhoncus cursus diam”, fundSource: “Test” } 我需要日期正确地出现在绑定的angular / bootstrap / date选择器中 。 我需要在将日期绑定到输入框时将日期转换为格式yyyy-mm-dd(没有时间)。 只是一个指向某些文档的指针,解释了从API序列化日期到角度的正确方法。 我确信effectiveDate实际上应该是Date对象而不是string 。 对于completness,返回json值的服务如下所示: app.factory(‘Service’, [‘$http’, ‘$location’, ‘$interpolate’, function ($http, $location, $interpolate) { return { get: function (account) { var url = ‘api/consultations/{account}’; […]

DateTime.TryParse无法解析DateTime.MinValue

我正在使用一个名为Json.NET的库,它在内部使用以下代码将JSON字符串解析为DateTime: if (DateTime.TryParse(s, Culture, DateTimeStyles.RoundtripKind, out dt)) { dt = DateTimeUtils.EnsureDateTime(dt, DateTimeZoneHandling); SetToken(JsonToken.Date, dt); return dt; } 我认为Json.NET搞砸了转换,但看起来它是DateTime.TryParse本身就是拙劣的价值。 当我解析以下有效的Iso日期(对应于UTC DateTime.MinValue)时: string json = “0001-01-01T00:00:00+00:00”; DateTime dt; DateTime.TryParse(json, invariantCulture, DateTimeStyles.RoundtripKind, out dt); 结果是本地化的DateTime: {0001-01-01 8:00:00 PM} ,当转换回Utc时间时, {0001-01-02 0:00:00 PM} 。 基本上,日期下溢,这正是您期望DateTimeStyles.RoundtripKind要避免的问题。 我该如何避免这种情况?

Web Api控制器无法识别Json方法

我有一个web api控制器 using sport.BLL.Abstract; using sport.BLL.Concrete; using sport.DAL.Entities; using sport.webApi.Models; using AutoMapper; using Microsoft.AspNet.Identity.EntityFramework; using System; using System.Collections.Generic; using System.Linq; using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.Owin; using Microsoft.Owin.Security; using System.Net; using System.Net.Http; using System.Web.Http; using System.Web; using System.Web.WebPages.Html; namespace sport.webApi.Controllers { public class AccountManageController : ApiController { [HttpPost] public System.Web.Mvc.ActionResult CreateAccount(CollaborateurModel item) { var user = new […]

使用C#反序列化JSON以返回项目

我有以下内容: {“documents”: [{“keyPhrases”: [ “search results”,”Azure Search”,”fast search indexing”,”sophisticated search capabilities”,”Build great search experiences”,”time-sensitive search scenarios”,”service availability”,”managed service”,”service updates”,”index corruption”,”near-instantaneous responses”,”multiple languages”,”integrated Microsoft natural language stack”,”multiple indexes”,”application changes”,”ranking models”,”great relevance”,”years of development”,”primary interaction pattern”,”storage”,”Bing”,”data volume”,”rich”,”suggestions”,”hassle of dealing”,”Reliable throughput”,”website”,”incremental cost”,”complexity”,”faceting”,”traffic”,”mobile apps”,”business goals”,”users”,”applications”,”user expectations”,”Office” ], “id”:”1″}], “errors”:[] } 我需要在keyPhrases中提取项目,但绝对不知道如何去做。 我尝试过以下方法: KeyPhraseResult keyPhraseResult = new KeyPhraseResult(); /// /// […]

将字符串转换为json

string []a=new string[2]; a[0]=”[“; a[1]=”/”; NET3.5 如何将a转换为json数组?通过C#

如何在C#中将Json对象转换为数组

我正在尝试将JSON对象转换为C#数组。 这是JSon I Getfrom the Server webrequest响应: string result = sr.ReadToEnd(); // this line get me response result = { “subjects”: [{ “subject_id”: 1, “subject_name”: “test 1”, “subject_class”: 4, “subject_year”: “2015”, “subject_code”: “t-1” },{ “subject_id”: 2, “subject_name”: “test 2”, “subject_class”: 5, “subject_year”: “”, “subject_code”: “t-2” }] }; dynamic item = JsonConvert.DeserializeObject(result); string iii = Convert.ToString(item[“subjects”]); […]