我刚接触[C#] Newtonsoft.Json.dll解析json,看教程做但是不够详细,希望有人能帮帮我,我是把代码写完了,也没有错误,只是教程上没说怎么添加控件,我逐个拖进去,做后运行,但是程序没有应,无法运行,也没给任何提示,有人可以教教我吗?谢谢啦。
教程链接:Newtonsoft.Json.dll解析json实例 - PHP、C#/学习-356网站建设交流社区
http://bbs.356web.com/thread-322-1-1.html
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace JsonTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ReadTxt();
}
private void button1_Click(object sender, EventArgs e)
{
ReadJson();
}
List<User> UserList;
public void ReadJson()
{
string UsersInfo = textBox1.Text;
if (UsersInfo == "")
{
MessageBox.Show("没有找到需要解析的数据!");
return;
}
UserList = new List<User>();
JObject jsonObj = JObject.Parse(UsersInfo);
JArray UserJa = JArray.Parse(jsonObj["Users"].ToString());
for (int i = 0; i < UserJa.Count; i++)
{
User user = new User();
user.Username = UserJa[i]["Username"].ToString();
user.Stu_number = UserJa[i]["Stu_number"].ToString();
user.Ssd = UserJa[i]["ssd"].ToString();
user.Key = UserJa[i]["Key"].ToString();
user.AreaId = UserJa[i]["areaId"].ToString();
user.Number = UserJa[i]["number"].ToString();
UserList.Add(user);
ListViewItem lvi = new ListViewItem(user.Username);
lvi.SubItems.Add(user.Stu_number);
lvi.SubItems.Add(user.Ssd);
lvi.SubItems.Add(user.Key);
lvi.SubItems.Add(user.AreaId);
lvi.SubItems.Add(user.Number);
this.listView1.Items.Add(lvi);
}
label1.Text = UserList[0].Username + " " + UserList[0].Stu_number + " " + UserList[0].Ssd + " " + UserList[0].Key;//你想获取某一个值用这种方法显示
}
/// <summary>
/// 读取存放json数据的Txt文件
/// </summary>
public void ReadTxt()
{
string strLine;
string path = Application.StartupPath + "//UserInfo.txt";
if(!File.Exists(path))
{
MessageBox.Show("指定文件不存在!");
return;
}
FileStream fs = new FileStream(path, FileMode.Open);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.GetEncoding("gb2312"));
strLine = sr.ReadLine();
while (strLine != null)
{
textBox1.Text = textBox1.Text+strLine;
strLine = sr.ReadLine();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace JsonTest
{
class User
{
string username;
string stu_number;
string ssd;
string key;
string areaId;
string number;
public string Username
{
get { return username; }
set { username = value; }
}
public string Stu_number
{
get { return stu_number; }
set { stu_number = value; }
}
public string Ssd
{
get { return ssd; }
set { ssd = value; }
}
public string Key
{
get { return key; }
set { key = value; }
}
public string AreaId
{
get { return areaId; }
set { areaId = value; }
}
public string Number
{
get { return number; }
set { number = value; }
}
}
}
------解决思路----------------------
楼主,你是出现异常无法运行了,还是按了按钮无反应?
你拖了控件是不错,但你的按钮确定是双击后生成的button1_Click事件吗?不会是直接将代码贴过来的吧?那样的话,按钮事件没注册当然没任何反应的。没注册的话注册一下看看。
public Form1()
{
InitializeComponent();
ReadTxt();
//注册一下按钮事件
button1.Click += button1_Click;
}
------解决思路----------------------
前面说的问题实现了吗?
textView 和listView你想写出什么东西呢?