I'm pretty new to C#, i have this code:
我是C#的新手,我有这个代码:
downloads obj = JsonConvert.DeserializeObject<downloads>(data);
foreach (KeyValuePair<string, client> kvp in obj.client)
{
Console.WriteLine("URL: " + kvp.Value.url);
Console.WriteLine("SHA1: " + kvp.Value.sha1);
matrix[0].Add(kvp.Value.url);
matrix[0].Add(kvp.Value.sha1);
}
with these classes:
与这些类:
class downloads
{
[JsonProperty("client")]
public Dictionary<string, client> client { get; set; }
}
class client
{
[JsonProperty("url")]
public string url { get; set; }
[JsonProperty("sha1")]
public string sha1 { get; set; }
}
It says me Object reference not set to an instance of an object but how can i create a reference and use that reference as a type? I mean, i create a reference to downloads this say:
它说我没有将对象引用设置为对象的实例但是如何创建引用并将该引用用作类型?我的意思是,我创建了一个下载的参考说:
downloads down = new downloads();
but now how can i assign a variable that json deserialize? here is the complete code: https://dotnetfiddle.net/F9mSvQ
但是现在我如何分配json反序列化的变量?这是完整的代码:https://dotnetfiddle.net/F9mSvQ
Partial JSON:
{
"downloads":
{
"client": { "sha1": "e80d9b3bf5085002218d4be59e668bac718abbc6" },
"server": { "sha1": "952438ac4e01b4d115c5fc38f891710c4941df29" }
}
}
2 个解决方案
#1
1
From the JSON downloads
itself should be dictionary on Client
objects OR contain several properties of type Client
like
从JSON下载本身应该是客户端对象上的字典或包含Client类型的几个属性
class Downloads
{
public Client client { get; set; }
public Client server { get; set; }
}
Debugging note: It is always good idea to serialize your class to see what result it produces - this would let you know if structure looks matching or not.
调试说明:序列化类以查看它产生的结果总是一个好主意 - 这会让你知道结构是否匹配。
#2
0
First of all, I suggest you to stick to traditional Naming Conventions
首先,我建议你坚持使用传统的命名约定
It will help you and others to see your code better and more clear.
它将帮助您和其他人更好,更清晰地看到您的代码。
Based on the class structure you have, here is the json format that you want.
根据您拥有的类结构,这里是您想要的json格式。
string json = @"{clients: {
'clientname1':{'sha1':'shahashhere','url':'urlhere'},
'clientname2':{'sha1':'shahashhere','url':'urlhere'}
}}";
Change the values as needed.
根据需要更改值。
Here is link to the https://dotnetfiddle.net/uedQ8i. I changed the classes name as I was confused while I was working on it.
这是https://dotnetfiddle.net/uedQ8i的链接。当我正在处理它时,我改变了类名称。
EDIT
Since OP doesn't have control of JSON string, then another class have to be made to contain downloads
object
由于OP无法控制JSON字符串,因此必须使另一个类包含下载对象
class MinecraftObject {
Downloads downloads {get;set;}
}
Deserializing the json string
反序列化json字符串
var obj = JsonConvert.DeserializeObject<MinecraftObject>(json);
var download = obj.downloads;
#1
1
From the JSON downloads
itself should be dictionary on Client
objects OR contain several properties of type Client
like
从JSON下载本身应该是客户端对象上的字典或包含Client类型的几个属性
class Downloads
{
public Client client { get; set; }
public Client server { get; set; }
}
Debugging note: It is always good idea to serialize your class to see what result it produces - this would let you know if structure looks matching or not.
调试说明:序列化类以查看它产生的结果总是一个好主意 - 这会让你知道结构是否匹配。
#2
0
First of all, I suggest you to stick to traditional Naming Conventions
首先,我建议你坚持使用传统的命名约定
It will help you and others to see your code better and more clear.
它将帮助您和其他人更好,更清晰地看到您的代码。
Based on the class structure you have, here is the json format that you want.
根据您拥有的类结构,这里是您想要的json格式。
string json = @"{clients: {
'clientname1':{'sha1':'shahashhere','url':'urlhere'},
'clientname2':{'sha1':'shahashhere','url':'urlhere'}
}}";
Change the values as needed.
根据需要更改值。
Here is link to the https://dotnetfiddle.net/uedQ8i. I changed the classes name as I was confused while I was working on it.
这是https://dotnetfiddle.net/uedQ8i的链接。当我正在处理它时,我改变了类名称。
EDIT
Since OP doesn't have control of JSON string, then another class have to be made to contain downloads
object
由于OP无法控制JSON字符串,因此必须使另一个类包含下载对象
class MinecraftObject {
Downloads downloads {get;set;}
}
Deserializing the json string
反序列化json字符串
var obj = JsonConvert.DeserializeObject<MinecraftObject>(json);
var download = obj.downloads;