如下所示的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"
}]
现将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);
}
}
然后将中文属性转换成对应的英文属性
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;
}
}
}