今天偶然知道,Vs自带Json转Entity的功能。
首先,复制你要转换的Json。
然后在新建一个类,准备输入:
VS最上面菜单栏点击,编辑->选择性粘贴->将Json转成类
版本好像要VS2012之后,因为听人说VS2010没有这个功能。
下面属于知识拓展,上面才是最简单的Json转实体的方法
首先,需要在NuGet中下载添加Newtonsoft.Json
先来一段字符串:
string jsonString = "{\"log_id\":119047692893297896,\"direction\":0,\"words_result_num\":30,\"words_result\":{\"InvoiceNum\":\"61109124\",\"SellerName\":\"北京智能科技有限公司\",\"CommodityTaxRate\":[{\"word\":\"16%\",\"row\":\"1\"},{\"word\":\"6%\",\"row\":\"2\"},{\"word\":\"10%\",\"row\":\"3\"},{\"word\":\"6%\",\"row\":\"4\"}],\"SellerBank\":\"中国工商银行北京市分行菜市口支行\",\"Checker\":\"王东\",\"TotalAmount\":\"25440.39\",\"CommodityAmount\":[{\"word\":\"18750.00\",\"row\":\"1\"},{\"word\":\"1367.92\",\"row\":\"2\"},{\"word\":\"3954.55\",\"row\":\"3\"},{\"word\":\"1367.92\",\"row\":\"4\"}],\"InvoiceDate\":\"2019年01月15日\",\"CommodityTax\":[{\"word\":\"3000.00\",\"row\":\"1\"},{\"word\":\"82.08\",\"row\":\"2\"},{\"word\":\"395.45\",\"row\":\"3\"},{\"word\":\"82.08\",\"row\":\"4\"}],\"PurchaserName\":\"北京鸿泰科技发展有限公司\",\"CommodityNum\":[{\"word\":\"\",\"row\":\"1\"},{\"word\":\"1\",\"row\":\"2\"},{\"word\":\"1\",\"row\":\"3\"},{\"word\":\"1\",\"row\":\"4\"}],\"PurchaserBank\":\"北京农村商业银行股份有限公司\",\"Remarks\":\"\",\"Password\":\"70<336<4-6864-7546>850211977>+9+/5>-++0+57+*/+>*>>6691-9>1>-5/4377<9699<>6+米762+744*6-><66-75米0>04*038<3901<\",\"SellerAddress\":\"北京市东城区广渠门内大街\",\"PurchaserAddress\":\"北京市海淀区苏州街\",\"InvoiceCode\":\"1100182\",\"CommodityUnit\":[{\"word\":\"台\",\"row\":\"1\"},{\"word\":\"套\",\"row\":\"2\"},{\"word\":\"套\",\"row\":\"3\"},{\"word\":\"套\",\"row\":\"4\"}],\"Payee\":\"王丹\",\"PurchaserRegisterNum\":\"91110108MA00W2J\",\"CommodityPrice\":[{\"word\":\"8750.00\",\"row\":\"1\"},{\"word\":\"1367.9245283\",\"row\":\"2\"},{\"word\":\"3954.5454545\",\"row\":\"3\"},{\"word\":\"1367.9245283\",\"row\":\"4\"}],\"NoteDrawer\":\"管理\",\"AmountInWords\":\"贰万玖仟圆整\",\"AmountInFiguers\":\"29000.00\",\"TotalTax\":\"3559.61\",\"InvoiceType\":\"专用发票\",\"SellerRegisterNum\":\"91110109675057H\",\"CommodityName\":[{\"word\":\"*非电力家用器具*壁挂炉\",\"row\":\"1\"},{\"word\":\"*设计服务*设计费\",\"row\":\"2\"},{\"word\":\"*建筑服务*安装费\",\"row\":\"3\"},{\"word\":\"*信息技术服务*调试费\",\"row\":\"4\"}],\"CommodityType\":[{\"word\":\"\",\"row\":\"1\"},{\"word\":\"\",\"row\":\"2\"},{\"word\":\"\",\"row\":\"3\"},{\"word\":\"\",\"row\":\"4\"}],\"CheckCode\":\"\"}}";
{}包裹的内容,需要转成JObject
[]包裹的内容,需要转成JArray
上面的字符串最外围是{},所以它的第一步转换就是:
JObject jsonObj = JObject.Parse(jsonString);
同样的,如果最外围是[],就需要用
JArray jsonArr = JArray.Parse(jsonString);
而下一步基本相同,就是
var model = jsonObj.ToObject();
或者
var models = jsonArr.ToObject>();
其中,Model就是和json一一对应的实体了,其中的属性,要和json的Key一模一样。
针对上面的字符串,我我的整个转化代码如下,其中涉及JArray的部分也有,虽然没有全部写全,但意思全部表达出来了:
Result 就是JObject中的下层JObject
List 就是JObject内部的JArray,需要写成List;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JsonDemo
{
public class Model
{
public string log_id { get; set; }
public int direction { get; set; }
public int words_result_num { get; set; }
public Result words_result { get; set; }
}
public class Result
{
public string InvoiceNum { get;set;}
public string SellerName { get; set; }
public List CommodityTaxRate { get; set; }
public string SellerBank { get; set; }
public string Checker { get; set; }
}
public class WordRow
{
public string word { get; set; }
public string row { get; set; }
}
class Program
{
static void Main(string[] args)
{
string jsonString = "{\"log_id\":119047692893297896,\"direction\":0,\"words_result_num\":30,\"words_result\":{\"InvoiceNum\":\"61109124\",\"SellerName\":\"北京智能科技有限公司\",\"CommodityTaxRate\":[{\"word\":\"16%\",\"row\":\"1\"},{\"word\":\"6%\",\"row\":\"2\"},{\"word\":\"10%\",\"row\":\"3\"},{\"word\":\"6%\",\"row\":\"4\"}],\"SellerBank\":\"中国工商银行北京市分行菜市口支行\",\"Checker\":\"王东\",\"TotalAmount\":\"25440.39\",\"CommodityAmount\":[{\"word\":\"18750.00\",\"row\":\"1\"},{\"word\":\"1367.92\",\"row\":\"2\"},{\"word\":\"3954.55\",\"row\":\"3\"},{\"word\":\"1367.92\",\"row\":\"4\"}],\"InvoiceDate\":\"2019年01月15日\",\"CommodityTax\":[{\"word\":\"3000.00\",\"row\":\"1\"},{\"word\":\"82.08\",\"row\":\"2\"},{\"word\":\"395.45\",\"row\":\"3\"},{\"word\":\"82.08\",\"row\":\"4\"}],\"PurchaserName\":\"北京鸿泰科技发展有限公司\",\"CommodityNum\":[{\"word\":\"\",\"row\":\"1\"},{\"word\":\"1\",\"row\":\"2\"},{\"word\":\"1\",\"row\":\"3\"},{\"word\":\"1\",\"row\":\"4\"}],\"PurchaserBank\":\"北京农村商业银行股份有限公司\",\"Remarks\":\"\",\"Password\":\"70<336<4-6864-7546>850211977>+9+/5>-++0+57+*/+>*>>6691-9>1>-5/4377<9699<>6+米762+744*6-><66-75米0>04*038<3901<\",\"SellerAddress\":\"北京市东城区广渠门内大街\",\"PurchaserAddress\":\"北京市海淀区苏州街\",\"InvoiceCode\":\"1100182\",\"CommodityUnit\":[{\"word\":\"台\",\"row\":\"1\"},{\"word\":\"套\",\"row\":\"2\"},{\"word\":\"套\",\"row\":\"3\"},{\"word\":\"套\",\"row\":\"4\"}],\"Payee\":\"王丹\",\"PurchaserRegisterNum\":\"91110108MA00W2J\",\"CommodityPrice\":[{\"word\":\"8750.00\",\"row\":\"1\"},{\"word\":\"1367.9245283\",\"row\":\"2\"},{\"word\":\"3954.5454545\",\"row\":\"3\"},{\"word\":\"1367.9245283\",\"row\":\"4\"}],\"NoteDrawer\":\"管理\",\"AmountInWords\":\"贰万玖仟圆整\",\"AmountInFiguers\":\"29000.00\",\"TotalTax\":\"3559.61\",\"InvoiceType\":\"专用发票\",\"SellerRegisterNum\":\"91110109675057H\",\"CommodityName\":[{\"word\":\"*非电力家用器具*壁挂炉\",\"row\":\"1\"},{\"word\":\"*设计服务*设计费\",\"row\":\"2\"},{\"word\":\"*建筑服务*安装费\",\"row\":\"3\"},{\"word\":\"*信息技术服务*调试费\",\"row\":\"4\"}],\"CommodityType\":[{\"word\":\"\",\"row\":\"1\"},{\"word\":\"\",\"row\":\"2\"},{\"word\":\"\",\"row\":\"3\"},{\"word\":\"\",\"row\":\"4\"}],\"CheckCode\":\"\"}}";
JObject jsonObj = JObject.Parse(jsonString);
var model = jsonObj.ToObject();
var temp = model;
}
}
}
顺便将Json字符串格式化一下,或许会更好看一点:
{
"log_id": 119047692893297896,
"direction": 0,
"words_result_num": 30,
"words_result": {
"InvoiceNum": "61109124",
"SellerName": "北京智能科技有限公司",
"CommodityTaxRate": [{
"word": "16%",
"row": "1"
}, {
"word": "6%",
"row": "2"
}, {
"word": "10%",
"row": "3"
}, {
"word": "6%",
"row": "4"
}],
"SellerBank": "中国工商银行北京市分行菜市口支行",
"Checker": "王东",
"TotalAmount": "25440.39",
"CommodityAmount": [{
"word": "18750.00",
"row": "1"
}, {
"word": "1367.92",
"row": "2"
}, {
"word": "3954.55",
"row": "3"
}, {
"word": "1367.92",
"row": "4"
}],
"InvoiceDate": "2019年01月15日",
"CommodityTax": [{
"word": "3000.00",
"row": "1"
}, {
"word": "82.08",
"row": "2"
}, {
"word": "395.45",
"row": "3"
}, {
"word": "82.08",
"row": "4"
}],
"PurchaserName": "北京鸿泰科技发展有限公司",
"CommodityNum": [{
"word": "",
"row": "1"
}, {
"word": "1",
"row": "2"
}, {
"word": "1",
"row": "3"
}, {
"word": "1",
"row": "4"
}],
"PurchaserBank": "北京农村商业银行股份有限公司",
"Remarks": "",
"Password": "70<336<4-6864-7546>850211977>+9+/5>-++0+57+*/+>*>>6691-9>1>-5/4377<9699<>6+米762+744*6-><66-75米0>04*038<3901<",
"SellerAddress": "北京市东城区广渠门内大街",
"PurchaserAddress": "北京市海淀区苏州街",
"InvoiceCode": "1100182",
"CommodityUnit": [{
"word": "台",
"row": "1"
}, {
"word": "套",
"row": "2"
}, {
"word": "套",
"row": "3"
}, {
"word": "套",
"row": "4"
}],
"Payee": "王丹",
"PurchaserRegisterNum": "91110108MA00W2J",
"CommodityPrice": [{
"word": "8750.00",
"row": "1"
}, {
"word": "1367.9245283",
"row": "2"
}, {
"word": "3954.5454545",
"row": "3"
}, {
"word": "1367.9245283",
"row": "4"
}],
"NoteDrawer": "管理",
"AmountInWords": "贰万玖仟圆整",
"AmountInFiguers": "29000.00",
"TotalTax": "3559.61",
"InvoiceType": "专用发票",
"SellerRegisterNum": "91110109675057H",
"CommodityName": [{
"word": "*非电力家用器具*壁挂炉",
"row": "1"
}, {
"word": "*设计服务*设计费",
"row": "2"
}, {
"word": "*建筑服务*安装费",
"row": "3"
}, {
"word": "*信息技术服务*调试费",
"row": "4"
}],
"CommodityType": [{
"word": "",
"row": "1"
}, {
"word": "",
"row": "2"
}, {
"word": "",
"row": "3"
}, {
"word": "",
"row": "4"
}],
"CheckCode": ""
}
}