I am new in json. I want information of different users and add them to a dataGridView or dataTable or dataSet in c# (.net development). Information sample is (The json is valid):
我是json的新人。我想要不同用户的信息,并将它们添加到c#(.net development)中的dataGridView或dataTable或dataSet。信息样本是(json有效):
{
"JrPwbApfIHbQhCUmVIoiVJcPYv93": {
"address": "Jessore",
"name": "Dev"
},
"iBRZAyn8TQTOgKTcByGOvJjL9ZB3": {
"address": "Bogra",
"name": "Kumar Saikat"
}
}
I want them like this :
我希望他们这样:
User1 | Jessore | Dev
User1 |杰索尔|开发
User2 | Bogra | Kumar Saikat
User2 | Bogra | Kumar Saikat
Even it would help if I could make a list for all of them.
如果我可以列出所有这些列表,它甚至会有所帮助。
I believe I was able to deserialise them (not sure at all) byvar model = JsonConvert.DeserializeObject<user>(json);
我相信我能够通过var model = JsonConvert.DeserializeObject
where user is a class.
用户是一个类。
public class Item
{
public string name;
public string address;
}
from this question-answer. From this tutorial I am able to get values if property is known. But in my case my property would be unknown, (string "User1","User2" would be random, since I will get them from a database). Any help and light on this matter would be greatly appreciated. Thank you in advance.
从这个问题的答案。在本教程中,如果知道属性,我可以获取值。但在我的情况下,我的属性将是未知的,(字符串“User1”,“User2”将是随机的,因为我将从数据库中获取它们)。任何有关此事的帮助和亮点将不胜感激。先谢谢你。
3 个解决方案
#1
6
You're looking at a JSON dictionary, so just deserialize it as such:
您正在查看JSON字典,因此只需将其反序列化:
public static Dictionary<string,Item> ParseJson(string source)
{
return JsonConvert.DeserializeObject<Dictionary<string,Item>>(source);
}
If you call it like this:
如果你这样称呼它:
public static void Main()
{
var input = @"{'JrPwbApfIHbQhCUmVIoiVJcPYv93': {'address': 'Jessore','name': 'Dev' }, 'iBRZAyn8TQTOgKTcByGOvJjL9ZB3': {'address': 'Bogra','name': 'Kumar Saikat'}}";
var result = ParseJson(input);
foreach (var r in result)
{
Console.WriteLine("Key={0};Name={1};Address={2}", r.Key, r.Value.name, r.Value.address);
}
}
The output is:
输出是:
Key=JrPwbApfIHbQhCUmVIoiVJcPYv93;Name=Dev;Address=Jessore
Key=iBRZAyn8TQTOgKTcByGOvJjL9ZB3;Name=Kumar Saikat;Address=Bogra
This example dumps the list to the console, but you could easily modify the for
loop to add to a list instead.
此示例将列表转储到控制台,但您可以轻松修改for循环以添加到列表中。
See my example on DotNetFiddle
看看我在DotNetFiddle上的例子
#2
0
Can use the nuget package Newtonsoft.Json. This code gives you what you are looking for:
可以使用nuget包Newtonsoft.Json。此代码为您提供所需内容:
using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
var json =
"{\"JrPwbApfIHbQhCUmVIoiVJcPYv93\":{\"address\":\"Jessore\",\"name\":\"Dev\"}," +
"\"iBRZAyn8TQTOgKTcByGOvJjL9ZB3\":{\"address\":\"Bogra\",\"name\":\"Kumar Saikat\"}}";
var o = JsonConvert.DeserializeObject(json);
var items = new List<Item>();
foreach (dynamic x in o as IEnumerable)
{
var i = new Item();
var y = x.First;
i.Name = y.name.Value;
i.Address = y.address.Value;
items.Add(i);
}
}
public class Item
{
public string Name { get; set; }
public string Address { get; set; }
}
}
}
Your situation is a bit strange as those autocreated names like "JrPwbApfIHbQhCUmVIoiVJcPYv93" or else it's easier, but should be fairly easy code.
您的情况有点奇怪,因为那些自动处理的名称如“JrPwbApfIHbQhCUmVIoiVJcPYv93”或者它更容易,但应该是相当简单的代码。
Keep in mind I use "dynamic" there which means problems will hit you at runtime NOT design time as it's not checked.
请记住,我在那里使用“动态”,这意味着问题会在运行时触及你而不是设计时间,因为它没有被检查。
#3
0
The correct way to deserialize would be as below
反序列化的正确方法如下
var model = JsonConvert.DeserializeObject<Dictionary<string, Item>>(data);
In the code sample you have posted, your "user" class name is Item but you are trying to deserialize using "User" in your code. Also please note that you cannot directly directly deserialize data into users list as it is present as a value of some random strings.
在您发布的代码示例中,您的“用户”类名称是Item,但您尝试使用代码中的“User”反序列化。另请注意,您不能直接将数据反序列化到用户列表中,因为它作为一些随机字符串的值存在。
var model = JsonConvert.DeserializeObject<user>(json);
For your code to deserialize correctly, your json format should be as below :
要使代码正确反序列化,您的json格式应如下所示:
{
{
"address": "Jessore",
"name": "Dev"
},
{
"address": "Bogra",
"name": "Kumar Saikat"
}
}
}
#1
6
You're looking at a JSON dictionary, so just deserialize it as such:
您正在查看JSON字典,因此只需将其反序列化:
public static Dictionary<string,Item> ParseJson(string source)
{
return JsonConvert.DeserializeObject<Dictionary<string,Item>>(source);
}
If you call it like this:
如果你这样称呼它:
public static void Main()
{
var input = @"{'JrPwbApfIHbQhCUmVIoiVJcPYv93': {'address': 'Jessore','name': 'Dev' }, 'iBRZAyn8TQTOgKTcByGOvJjL9ZB3': {'address': 'Bogra','name': 'Kumar Saikat'}}";
var result = ParseJson(input);
foreach (var r in result)
{
Console.WriteLine("Key={0};Name={1};Address={2}", r.Key, r.Value.name, r.Value.address);
}
}
The output is:
输出是:
Key=JrPwbApfIHbQhCUmVIoiVJcPYv93;Name=Dev;Address=Jessore
Key=iBRZAyn8TQTOgKTcByGOvJjL9ZB3;Name=Kumar Saikat;Address=Bogra
This example dumps the list to the console, but you could easily modify the for
loop to add to a list instead.
此示例将列表转储到控制台,但您可以轻松修改for循环以添加到列表中。
See my example on DotNetFiddle
看看我在DotNetFiddle上的例子
#2
0
Can use the nuget package Newtonsoft.Json. This code gives you what you are looking for:
可以使用nuget包Newtonsoft.Json。此代码为您提供所需内容:
using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
var json =
"{\"JrPwbApfIHbQhCUmVIoiVJcPYv93\":{\"address\":\"Jessore\",\"name\":\"Dev\"}," +
"\"iBRZAyn8TQTOgKTcByGOvJjL9ZB3\":{\"address\":\"Bogra\",\"name\":\"Kumar Saikat\"}}";
var o = JsonConvert.DeserializeObject(json);
var items = new List<Item>();
foreach (dynamic x in o as IEnumerable)
{
var i = new Item();
var y = x.First;
i.Name = y.name.Value;
i.Address = y.address.Value;
items.Add(i);
}
}
public class Item
{
public string Name { get; set; }
public string Address { get; set; }
}
}
}
Your situation is a bit strange as those autocreated names like "JrPwbApfIHbQhCUmVIoiVJcPYv93" or else it's easier, but should be fairly easy code.
您的情况有点奇怪,因为那些自动处理的名称如“JrPwbApfIHbQhCUmVIoiVJcPYv93”或者它更容易,但应该是相当简单的代码。
Keep in mind I use "dynamic" there which means problems will hit you at runtime NOT design time as it's not checked.
请记住,我在那里使用“动态”,这意味着问题会在运行时触及你而不是设计时间,因为它没有被检查。
#3
0
The correct way to deserialize would be as below
反序列化的正确方法如下
var model = JsonConvert.DeserializeObject<Dictionary<string, Item>>(data);
In the code sample you have posted, your "user" class name is Item but you are trying to deserialize using "User" in your code. Also please note that you cannot directly directly deserialize data into users list as it is present as a value of some random strings.
在您发布的代码示例中,您的“用户”类名称是Item,但您尝试使用代码中的“User”反序列化。另请注意,您不能直接将数据反序列化到用户列表中,因为它作为一些随机字符串的值存在。
var model = JsonConvert.DeserializeObject<user>(json);
For your code to deserialize correctly, your json format should be as below :
要使代码正确反序列化,您的json格式应如下所示:
{
{
"address": "Jessore",
"name": "Dev"
},
{
"address": "Bogra",
"name": "Kumar Saikat"
}
}
}