英文:
Can someone help me use this Json file in c#?
问题
我已经尝试了6个小时,试图在文本框中显示JSON文件的内容,但一无所获。以下是C#代码:
private thruster Getthruster()
{
string text = File.ReadAllText(@"./thrusters.json");
List<thruster> test = JsonConvert.DeserializeObject<List<thruster>>(text);
textBox1.Text = text;
foreach (var item in test)
{
textBox1.Text = item.type;
}
return Getthruster();
}
public class thruster
{
public string id { get; set; }
public string type { get; set; }
public string placement { get; set; }
}
{
"thruster": [
{
"id": 1,
"type": "tunnel",
"placement": "bow"
},
{
"id": 2,
"type": "tunnel",
"placement": "bow"
},
{
"id": 3,
"type": "azimuth",
"placement": "bow"
},
{
"id": 5,
"type": "azimuth",
"placement": "stern"
},
{
"id": 5,
"type": "tunnel",
"placement": "stern"
},
{
"id": 6,
"type": "azimuth_propulsion"
},
{
"id": 7,
"type": "azimuth_propulsion"
}
]
}
英文:
I have been trying to display contents of a json file in a textbox for the past 6 hours and got nowhere.
here's the c# code
private thruster Getthruster()
{
string text = File.ReadAllText(@"./thrusters.json");
List<thruster> test = JsonConvert.DeserializeObject<List<thruster>>(text);
textBox1.Text = text;
foreach (var item in test)
{
textBox1.Text = item.type;
}
return Getthruster();
}
public class thruster
{
public string id { get; set; }
public string type { get; set; }
public string placement { get; set; }
}
{
"thruster": [
{
"id": 1,
"type": "tunnel",
"placement": "bow"
},
{
"id": 2,
"type": "tunnel",
"placement": "bow"
},
{
"id": 3,
"type": "azimuth",
"placement": "bow"
},
{
"id": 5,
"type": "azimuth",
"placement": "stern"
},
{
"id": 5,
"type": "tunnel",
"placement": "stern"
},
{
"id": 6,
"type": "azimuth_propulsion"
},
{
"id": 7,
"type": "azimuth_propulsion"
}
]
}
答案1
得分: 1
开始时,您遇到了StackOverflowException
,因为您在同一个方法内部调用了相同的方法(Getthruster()
),而且没有任何退出条件。
之后,您的 JSON 文件似乎是不正确的。您在那里有一个Dictionary<string,thruster[]>
,而不是thruster[]
(或 List)。
您的 thruster[]
或 List<thruster>
的 JSON 应该是这样的:
[
{
"id": "1",
"type": "t",
"placement": "top"
},
{
"id": "2",
"type": "t",
"placement": "top"
}
]
英文:
To start, you are running into a StackOverflowException
since you are calling the same method (Getthruster()
) inside the same method without any exit condition.
After this, your json file seems to be incorrect. You have there a Dictionary<string,thruster[]>
and not a thruster[]
(Or List).
Your json for a thruster[]
or List<thruster>
should be something like:
[
{
"id": "1",
"type": "t",
"placement": "top"
},
{
"id": "2",
"type": "t",
"placement": "top"
}
]
答案2
得分: 0
你在一个JSON字符串中有一个对象,但你尝试将其序列化为数组。你可以使用以下代码来反序列化JSON中的数组:
List<thruster> test = JObject.Parse(text)["thruster"].ToObject<List<thruster>>();
英文:
you have an object inside of a json string, but you try to serialize it as it is an array. You can use this code that deserializes an array inside of your json
List<thruster> test = JObject.Parse(text)["thruster"].ToObject<List<thruster>>();
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论