Tag: json

Facebook JSON字符串的反序列化?

我无法解决从运行我的fql查询后生成的JSON字符串中提取facebook用户的work_history,affiliations和current_loc的问题。 我做了这堂课: public class Data { public CurrentLocation current_location { get; set; } public WorkHistory[] work_history { get; set; } public EducationHistory[] education_history { get; set; } } public class EducationHistory { public string name { get; set; } public int? year { get; set; } public string school_type { get; set; } } public […]

用JSON解析C#字典

我正在尝试使用JSON从我的C#应用​​程序中检索键/值对的字典,但我在某个地方搞砸了。 这是我第一次使用JSON,所以我可能只是在做一些愚蠢的事情。 C#代码: else if (string.Equals(request, “getchat”)) { string timestamp = DateTime.Now.ToString(“yyyy.MM.dd hh:mm:ss”); Dictionary data = new Dictionary(); data.Add(timestamp, “random message”); data.Add(timestamp, “2nd chat msg”); data.Add(timestamp, “console line!”); return Response.AsJson(data); } 使用Javascript: function getChatData() { $.getJSON(dataSource + “?req=getchat”, “”, function (data) { $.each(data, function(key, val) { addChatEntry(key, val); } }); }

使用JSON.NET在C#中将动态JSON字符串解析为字符串

这是我在C#和JSON上的第一个小项目。 我被要求将JSON文件解析为以下内容:我正在尝试创建一个窗体,其主体将包含以下格式的JSON字符串的内容: Name of the object (Label) name of the attribute of the object – (TextBox) editable value (Label) name of the attribute of the object – (TextBox) editable value … 我在json文件中有每个对象约35个属性和8个对象。 总共有大约50种不同的属性。 我搜索了JSON – C# – JSON.NET并阅读了超过15个问题和答案。 对于像我这样的初学者来说,答案有一些有价值的信息,但我无法将答案与我的情况联系起来。 原因如下: 一些答案将json字符串转换为C#对象。 因为我需要对象属性的“名称”,所以这个选项并不能解决我的问题。 我不知道如何在源代码中获取变量的名称并在Windows窗体中使用它。 一些答案将json字符串转换为数据结构。 我并不完全了解字典及其属性,但根据定义,我认为,字典会将2个值相互映射。 在我的例子中,3-5个不同的对象可能具有相同的属性,因此对于一个属性,将有多个值。 除此之外,我见过的一个例子是: var dict = new JavaScriptSerializer().Deserialize<Dictionary>(json); var postalCode = […]

使用WCF使用RESTful JSON API

我是WCF的新手(并且在.NET中一般都很生疏)所以有一个明显的机会这是一个已回答的问题而我错过了它。 我正在构建一个ASP.NET MVC应用程序,它将为其后端使用基于RESTful JSON的API。 我一直在研究如何在.NET中使用这种API的不同选项,看起来WCF是迄今为止最受欢迎的选择。 稍微阅读WCF我现在有一个基本的消费者类来发出请求,这是一个开始。 但现在我需要做更多的事情,而且我没有取得多大进展。 我需要使用JSON正文向API发送POST。 这是我到目前为止所拥有的: using System.ServiceModel; using System.ServiceModel.Web; using System.Runtime.Serialization; namespace APIConsumer { [ServiceContract] public interface IAPIClient { [OperationContract] [WebInvoke( Method = “POST”, BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = “/session/login.json” )] string SessionLogin(string login_name); } public class APIClient : ClientBase, IAPIClient { public string SessionLogin(string […]

WebService不反序列化某些对象

我已经建立了一个Web服务(REST),我从Windows服务中调用它。 有些电话似乎没有问题,也有问题。 而其他一些人正在返回Bad Request。 我将服务器设置为在反序列化数据为NULL时返回Bad Request。 Web Service使用EDMX从数据库生成对象类。 本地服务具有对Web服务的服务引用,因此它具有相同的对象类。 我正在使用Microsoft.AspNet.WebApi.Client Nuget Package来从本地服务进行调用。 代码低于Web服务操作 //NOT WORKING [OperationContract] [WebInvoke(Method = “POST”, UriTemplate = “{entity}/consent/{consent_id}/info”)] bool ConsentInformation(string entity, string consent_id, consent info); //WORKING [OperationContract] [WebInvoke(Method = “POST”, UriTemplate = “{entity}/council/users”)] void GoGetUsers(string entity, IEnumerable user); WEB服务代码 //NOT WORKING public void ConsentStatus(string entity, string consent_id, consent_status status) { Utilities.SetResponseFormat(); […]

如何正确使用javascript反序列化将json字符串转换为复杂对象?

我有我的.ashx处理程序中可用的以下json对象(var items =): {“Coverages”:{“PersonID”:10,”DetCode”:””,”Reimbursement”:””,”Deductible”:””,”MaximumPerAnnum”:””,”MaximumPerVisit”:””,”MaximumPerVisits”:””,”SvcCode”:””},”CoverageCombinedMaximums”:{“PersonID”:10,”DetCode”:[“AAAAA”,”BBBBB”,”CCCCC”],”MaximumPerAnnum”:””}} public void ProcessRequest (HttpContext context) { bool isSuccessful = true; var items = context.Request.Params[“items”]; if (isSuccessful) { JavaScriptSerializer ser = new JavaScriptSerializer(); AdditionalCoveragesPackage package = ser.Deserialize(items); } else { SendErrorMessage(context); return; } } 这是我试图反序列化的类的结构: public class AdditionalCoverage { public int PersonID { get; set; } public string DetCode { get; set; […]

如何将文件路径数组转换为分层JSON结构

我正在尝试在给定其所有文件和路径的数组的情况下创建机器目录结构的JSON。 该数组看起来像这样: string[] dirArray = { “./proc/15/task/15/exe”, “./proc/15/task/15/mounts/mounts.xml”, “./proc/15/task/15/mountinfo/mountinfo.xml”, “./proc/15/task/15/clear_refs/clear_ref.xml”, “./proc/14/loginuid/loginuid.xml”, “./proc/14/sessionid/sessionid.xml”, “./proc/14/coredump_filter/coredump_filter.xml”, “./proc/14/io/io.xml” } 我瞄准的目标JSON是这样的: { “.”: { “file”: { “name”:”fileInRoot.xml” }, “proc”: { “file”: { “name”:”fileInProc.xml” }, “15”: { “file”: { “name”:”fileIn15.xml” }, “task”: { “file”: { “name”:”fileInTask.xml” }, “15”: { “file”: { “name”:”fileInTask.xml” }, “mounts”: { “file”: { “name”:”fileInMounts.xml” } }, “mountsInfo”: […]

有没有办法使用HttpPostedFile类获得上传进度?

我想使用HttpPostedFile类从网页上传一个或多个大文件到ASP.NET MVC控制器。 使用此类,上载的大于256 KB的文件将缓冲到磁盘,而不是保存在服务器内存中。 我的理解是它可以这样做: if (context.Request.Files.Count > 0) { string tempFile = context.Request.PhysicalApplicationPath; for(int i = 0; i 0) { uploadFile.SaveAs(string.Format(“{0}{1}{2}”, tempFile,”Upload\\”, uploadFile.FileName)); } } } 有没有办法设置回调或使用其他方法,通过AJAX或JSON定期返回状态到网页,以便显示进度条和完成百分比? 代码会是什么样的?

JSON.NET反序列化属性名称转换为具有自定义ContractResolver的ExpandoObject

我有这个JSON: {“firstName”: “John”,”lastName”: “Doe”} 这个JSON.NET合同解析器: public class CustomContractResolver : DefaultContractResolver{ protected override string ResolvePropertyName(string propertyName) { return propertyName.Replace(“_”,””); } } 我有这个WebApi Controller方法,使用expando来使用提供的字段部分更新db行: public virtual int Post(int id, JObject content) { var obj = JsonConvert.DeserializeObject(content.ToString(), new JsonSerializerSettings { ContractResolver = new CustomContractResolver() }); db.Update(id, obj) } 我希望反序列化的expando具有属性first_name和last_name以匹配我的模型/ db列名,但是它的属性仍然匹配JSON。 直接反序列化为Person有first_name和last_name有效,正如LB帮助我在下面发现的那样,但是我的db层需要一个Expando来进行部分记录更新,否则它会吹掉没有被json指定的Person任何属性,因此返回null在模型中。 我可以在ContractResolver中做什么来转换Expando的属性?

SQL Server 2008 R2的C#CLR中的newtonsoft.json解析器 – 如何部署?

我创建了一个使用newtonsoft json parser.的CLR newtonsoft json parser. 我需要部署CLR 。 我为解析器依赖创建了一个程序集: CREATE ASSEMBLY [Newtonsoft.Json] FROM ‘C:\share\Release\Newtonsoft.Json.dll’ WITH PERMISSION_SET = UNSAFE 上面的语句抛出一个错误: `Assembly ‘Newtonsoft.Json’ references assembly ‘system.runtime.serialization, version=3.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089.’, which is not present in the current database. SQL Server attempted to locate and automatically load the referenced assembly from the same location where referring assembly came from, […]