//json转对象、数组, 反序列化 public static ReportDto JSONStringToList(string JsonStr) { //json格式字符串 JavaScriptSerializer Serializer = new JavaScriptSerializer(); //json字符串转为对象, 反序列化 ReportDto reportDto = Serializer.Deserialize <ReportDto>(JsonStr); return(reportDto); /* * //json格式字符串 * string JsonStrs = "[{Name:'苹果',Price:5.5},{Name:'橘子',Price:2.5},{Name:'柿子',Price:16}]"; * * JavaScriptSerializer Serializers = new JavaScriptSerializer(); * * //json字符串转为数组对象, 反序列化 * List<obj> objs = Serializers.Deserialize<list<obj>>(JsonStrs); * * foreach (var item in objs) * { * Console.Write(item.Name + ":" + item.Price + "\r\n"); * } */ }
/// <summary> /// 开启报告 /// </summary> /// <param name="postDataStr"></param> private static void CreateReport(string postDataStr) { string postUrl = "http://hk-server.bisahealth.com/l/create_report_public"; //post方法返回json格式返回值 string jsonResult = HttpUtils.HttpPost(postUrl, postDataStr); ReportDto reportDto = new ReportDto(); //将json返回值转换成ReportDto对象 reportDto = JsonToObjectUtils.JSONStringToList(jsonResult.ToString()); Console.WriteLine(jsonResult); //当返回状态码为205和402时,才会有报告返回 if (reportDto.code == 205) { Console.WriteLine("Code:" + reportDto.code + "|Msg:" + reportDto.message + "\r\n"); Console.WriteLine("开启报告成功!"); } else if (reportDto.code == 402) { Console.WriteLine(reportDto.code + ":" + reportDto.message + "\r\n" + reportDto.appReport.report_number); int user_guid = 729; string report_number = reportDto.appReport.report_number; int report_status = 6; /* * 以防万一,我增加了一个手动修改报告状态的接口,设置报告状态status=6,即为失效状态。 * 如果开启报告一直报402,并返回了未上传数据的报告,此时你可以进行两种操作: * 如需要使用该方法,请去掉注释,再启动。 */ //1、补充上传这个报告的心电数据 //2、设置这个报告的状态为失效report_status=6(用下面这个ChangeReportStatus方法) //ChangeReportStatus(user_guid, report_number, report_status); } else { Console.WriteLine("Code:" + reportDto.code + "|Msg:" + reportDto.message + "\r\n"); } Console.ReadKey(); /* * }catch(Exception e) * { * Console.WriteLine(e.StackTrace); * } * finally * { * Console.ReadKey(); * } */ }
public static void ChangeReportStatus(int user_guid, string report_number, int report_status) { string httpGetUrl = "http://hk-server.bisahealth.com/l/change_report_status"; string getDataStr = "user_guid=" + user_guid + "&report_number=" + report_number + "&report_status=" + report_status; string jsonResult = HttpUtils.HttpGet(httpGetUrl, getDataStr); ReportDto reportDto = new ReportDto(); reportDto = JsonToObjectUtils.JSONStringToList(jsonResult.ToString()); if (reportDto.code == 200) { Console.WriteLine("修改状态成功!修改状态为" + report_status); } else { Console.WriteLine(jsonResult); } Console.ReadKey(); }
/* * static void Main(string[] args) * { * string jsonStr = "{\"code\":205,\"message\":\"Operation successfully\",\"appReport\":{\"id\":0,\"report_number\":\"f7a7214d355514688fb8bdd3e4303f66\"," + * "\"report_type\":11,\"report_status\":4,\"start_time\":1514260672011,\"end_time\":1514260762252,\"user_guid\":729}}"; * * /* * string objTOJson = GetJsonString(); * Console.WriteLine(objTOJson); * * Console.ReadKey(); */ /* * JSONStringToList(jsonStr); * * * } */ //对象与数组转JSON public static string GetJsonString() { //初始化对象 AppReport aReport = new AppReport() { id = 1, report_number = "sdkjfslfj", report_type = 10, report_status = 4, start_time = 1514260672011, end_time = 1514260672011, user_guid = 729 }; ReportDto reportDto = new ReportDto() { code = 200, message = "success", appReport = aReport }; //序列化 string objTOJson = new JavaScriptSerializer().Serialize(reportDto); return(objTOJson); //数组转json /* * List<obj> products = new List<obj>(){ * new Obj(){Name="苹果",Price=5.5}, * new Obj(){Name="橘子",Price=2.5}, * new Obj(){Name="干柿子",Price=16.00} * }; * * ProductList productlist = new ProductList(); * productlist.GetProducts = products; * //序列化 * string os = new JavaScriptSerializer().Serialize(productlist); * //输出 "{\"GetProducts\":[{\"Name\":\"苹果\",\"Price\":5.5},{\"Name\":\"橘子\",\"Price\":2.5},{\"Name\":\"干柿子\",\"Price\":16}]}" */ }