using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
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;
namespace Testjson
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
#region
//{"rows":[[{"NumIid":"001"},{ "Title":"xxxxx"},{ "Price":"hsj"},{ "OuterId":"xxxx"}],[{"NumIid":"002"},{ "Title":"xxxxx"},{ "Price":"xjh"},{ "OuterId":"xxxx"}],[{"NumIid":"003"},{ "Title":"xxxxx"},{ "Price":"llf"},{ "OuterId":"xxxx"}]]}
private void test1() {
JObject jo = (JObject)JsonConvert.DeserializeObject(richTextBox1.Text);
JArray arr = (JArray)jo["rows"];
for (int i = 0; i < arr.Count; i++)
{
JArray arr2 = (JArray)arr[i];
for (int j = 0; j < arr2.Count; j++)
{
JObject obj = (JObject)arr2[j];
MessageBox.Show(obj["Price"] + "");
}
}
}
#endregion
private void button1_Click(object sender, EventArgs e)
{
test();
}
private void test() {
gekou s = (gekou)getObj<gekou>(richTextBox1.Text);
MessageBox.Show(s.msg);
data ss = (data)getObj<data>(s.data.ToString());
MessageBox.Show(ss.schemeId);
JArray arr = (JArray)ss.latticeSchemeList;
JObject obj = (JObject)arr[0];
latticeSchemeList sss = (latticeSchemeList)getObj<latticeSchemeList>(obj.ToString());
MessageBox.Show(sss.inOutFlag);
}
public object getObj<T>(string responseText)
{
try
{
if (typeof(T).FullName == "System.DBNull")
{
return JsonConvert.DeserializeObject(responseText);
}
else
{
return JsonConvert.DeserializeObject(responseText, typeof(T));
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return responseText;
}
}
}
public class gekou
{
public string code;
public string msg;
public object data;
}
public class data {
public string serverTime;
public string extend1;
public string extend2;
public string extend3;
public string schemeId;
public string optFlag;
public object latticeSchemeList;
}
public class latticeSchemeList {
public string inOutFlag;
public string optFlag;
public string schemeId;
public string status;
public string shareFlag;
}
}
字串原文如下
{"code":"000","data":{"extend1":"","extend2":"","extend3":"","latticeSchemeList":[{"inOutFlag":"JG","optFlag":"1","schemeId":"JCD2109010001906316-01","shareFlag":"0","status":"1"}],"optFlag":"1","schemeId":"JCD2109010001906316-01","serverTime":"2021-04-15 10:07:27.703"},"msg":"成功"}