VS2017+C#
using System;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
namespace WindowsFormsAppIDcard
{
public partial class 身份证信息查询 : Form
{
private string strhome_location;
public 身份证信息查询()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string IDcard1;
IDcard1 = textBox1.Text;
Console.WriteLine(IDcard1.GetType());//判断类型
//判断地区
string strDist = IDcard1.Substring(0, 6);//截取前6位
//读取身份于号码对应的json文件
System.IO.StreamReader file = System.IO.File.OpenText(@"F:\SoftwareLearning\VS2017\C#Learing\chapter01\ConsoleApp0721\province.json");
JsonTextReader reader = new JsonTextReader(file);
//进行字符串切割赋值
JObject home_location = (JObject)JToken.ReadFrom(reader);
label1.Text = home_location[IDcard1.Substring(0, 6)].ToString();
//输出出生日期
string strYearDate = IDcard1.Substring(6, 4);//截取第6-10位
string strMonthDate = IDcard1.Substring(10, 2);//截取第6-10位
string strDate = IDcard1.Substring(12, 2);//截取第6-10位
label5.Text = strYearDate + '年' + strMonthDate + '月' + strDate + '日';
//判断性别
string strSex = IDcard1.Substring(16, 1);//截取倒数第二位
// Console.WriteLine("身份证号码倒数第二位:");
//Console.WriteLine(strSex);
int numSex = int.Parse(strSex); //字符串型变为数值型
if (numSex % 2 == 0)
{
label7.Text="女";
}
else
{
label7.Text = "男";
}
}
private void label2_Click(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
private void label4_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void 身份证信息查询_Load(object sender, EventArgs e)
{
}
}
}
运行结果:
这中间需要用到Json插件,安装过程见
其中身份证的信息.json文件下载
链接:https://pan.baidu.com/s/1_PkEcemNdf7SoqWAqt0W_g
提取码:hsy5