Json字符串解析C#行和列
我从Web服务获得以下JSON字符串Json字符串解析C#行和列,c#,json,C#,Json,我从Web服务获得以下JSON字符串 想通过C#in for循环读取行和列中的JSON字符串吗 我是C#对象编码方面的新手,如果下面的Json字符串的任何示例都能提供更多帮助的话 首先创建模型。例如,您可以使用Newtonsoft.Json的JsonConvert.DeserializeObject方法对Json进行反序列化 static void Main(string[] args) { string json = "[{\"_id\":\"1 - 1\",\"awid\":\"1
想通过C#in for循环读取行和列中的JSON字符串吗 我是C#对象编码方面的新手,如果下面的Json字符串的任何示例都能提供更多帮助的话
首先创建模型。例如,您可以使用
Newtonsoft.Json
的JsonConvert.DeserializeObject
方法对Json进行反序列化
static void Main(string[] args)
{
string json = "[{\"_id\":\"1 - 1\",\"awid\":\"1\",\"officeid\":\"1\",\"prname\":\"ABCD\",\"prfhname\":\"Chevy\",\"Ano\":\"555\"},{ \"_id\":\"1-2\",\"awid\":\"1\",\"officeid\":\"1\",\"prname\":\"bheegi\",\"prfhname\":\"Henry\",\"Ano\":\"6555\"}]";
var message = JsonConvert.DeserializeObject<Message[]>(json);
// To see the output (using the for loop as you like ) use this:
for (int i = 0; i < message.Length; i++)
{
Console.WriteLine("_id: " + message[i]._id);
Console.WriteLine("awid: " + message[i].awid);
Console.WriteLine("officeid: " + message[i].officeid);
Console.WriteLine("prname: " + message[i].prname);
Console.WriteLine("prfhname: " + message[i].prfhname);
Console.WriteLine("Ano: " + message[i].Ano);
Console.WriteLine();
}
Console.Read();
}
class Message
{
public string _id { get; set; }
public string awid { get; set; }
public string officeid { get; set; }
public string prname { get; set; }
public string prfhname { get; set; }
public string Ano { get; set; }
}
您尝试过任何东西吗?从我是C#对象编码新手开始如果给定Json字符串的任何示例更有用,您可以将数据反序列化到
字典中
,然后对其进行迭代,这很简单。
static void Main(string[] args)
{
string json = "[{\"_id\":\"1 - 1\",\"awid\":\"1\",\"officeid\":\"1\",\"prname\":\"ABCD\",\"prfhname\":\"Chevy\",\"Ano\":\"555\"},{ \"_id\":\"1-2\",\"awid\":\"1\",\"officeid\":\"1\",\"prname\":\"bheegi\",\"prfhname\":\"Henry\",\"Ano\":\"6555\"}]";
var message = JsonConvert.DeserializeObject<Message[]>(json);
// To see the output (using the for loop as you like ) use this:
for (int i = 0; i < message.Length; i++)
{
Console.WriteLine("_id: " + message[i]._id);
Console.WriteLine("awid: " + message[i].awid);
Console.WriteLine("officeid: " + message[i].officeid);
Console.WriteLine("prname: " + message[i].prname);
Console.WriteLine("prfhname: " + message[i].prfhname);
Console.WriteLine("Ano: " + message[i].Ano);
Console.WriteLine();
}
Console.Read();
}
class Message
{
public string _id { get; set; }
public string awid { get; set; }
public string officeid { get; set; }
public string prname { get; set; }
public string prfhname { get; set; }
public string Ano { get; set; }
}
_id: 1 - 1
awid: 1
officeid: 1
prname: ABCD
prfhname: Chevy
Ano: 555
_id: 1-2
awid: 1
officeid: 1
prname: bheegi
prfhname: Henry
Ano: 6555