• Json 序列化为Dictionary


    如下所示的json字符串中包含中文属性转换成英文属性

    ["sid":"dd1312","success":true,"data":{
      "发票代码": "032001500XXX",
      "发票号码": "25272XXX",
      "开票日期": "2017年XX月XX日",
      "校验码": "15922733756000XXXXXX",
      "发票金额": "XXX.00",
      "发票税额": "XXX.43",
      "不含税金额": "XXX.57",
      "受票方名称": "XXX",
      "受票方税号": "XXXX",
      "受票方地址、电话": "XXX",
      "受票方开户行、账号": "XXX",
      "销售方名称": "XXX",
      "销售方税号": "913201043023368XXX",
      "销售方地址、电话": "XXX",
      "销售方开户行、账号": "XXX"
    }]
    View Code

    现将json字符串转换成Dictionary类型

     /// <summary>
            /// 将Json数据反序列化为Dictionary
            /// </summary>
            /// <param name="jsonData">json数据</param>
            /// <returns></returns>
            public Dictionary<string, object> JsonToDictionary(string jsonData)
            {
                //实例化JavaScriptSerializer类的新实例
                JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
                try
                {
                    //将指定的 JSON 字符串转换为 Dictionary<string, object> 类型的对象
                    return javaScriptSerializer.Deserialize<Dictionary<string, object>>(jsonData);
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
    View Code

    然后将中文属性转换成对应的英文属性

    Dictionary<string,object> dic = JsonToDictionary(ress);
                InvoiceAliOcr res = new InvoiceAliOcr();
                res.data = new Ocr();
                if (dic != null &&dic.ContainsKey("data"))
                {
                    Dictionary<string, object> dataSet = (Dictionary<string, object>)dic["data"];
                    foreach (KeyValuePair<string, object> item in dataSet)
                    {
                        switch (item.Key)
                        {
                            case "发票代码":
                                res.data.fpdm = item.Value.ToString();
                                break;
                            case "发票号码":
                                res.data.fphm = item.Value.ToString();
                                break;
                            case "开票日期":
                                res.data.kprq = string.IsNullOrEmpty(item.Value.ToString()) ? "" : Convert.ToDateTime(item.Value.ToString()).ToString("yyyyMMdd");
                                break;
                            case "校验码":
                                res.data.code = item.Value.ToString();
                                break;
                            case "发票金额":
                                res.data.sumamount = item.Value.ToString();
                                break;
                            case "发票税额":
                                res.data.taxamount = item.Value.ToString();
                                break;
                            case "不含税金额":
                                res.data.notaxamount = item.Value.ToString();
                                break;
                            case "受票方名称":
                                res.data.gfMc = item.Value.ToString();
                                break;
                            case "受票方税号":
                                res.data.gfCode = item.Value.ToString();
                                break;
                            case "销售方名称":
                                res.data.sfMc = item.Value.ToString();
                                break;
                            case "销售方税号":
                                res.data.sfCode = item.Value.ToString();
                                break;
                            default:
                                break;
                        }
                    }
                }
    View Code
  • 相关阅读:
    uva 12426 Counting Triangles 计算几何
    poj 1195 Mobile phones 二维树状数组
    poj 1039 Pipe 计算几何
    poj 3580 SuperMemo 数据结构
    poj 1031 Fence 计算几何
    ArcEngine 无法嵌入互操作类型
    IDL 读取显示HDF文件
    Sql Server 2005 Com+ 警告处理办法
    C# 自定义控件开发
    ArcEngine 获取HDF文件中的子文件
  • 原文地址:https://www.cnblogs.com/ZJ199012/p/9970113.html
Copyright © 2020-2023  润新知