有人能帮我在C#中使用这个JSON文件吗?

huangapple go评论67阅读模式
英文:

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(@&quot;./thrusters.json&quot;);
            List&lt;thruster&gt; test = JsonConvert.DeserializeObject&lt;List&lt;thruster&gt;&gt;(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; }
        }
{
  &quot;thruster&quot;: [
    {
      &quot;id&quot;: 1,
      &quot;type&quot;: &quot;tunnel&quot;,
      &quot;placement&quot;: &quot;bow&quot;
    },
    {
      &quot;id&quot;: 2,
      &quot;type&quot;: &quot;tunnel&quot;,
      &quot;placement&quot;: &quot;bow&quot;
    },
    {
      &quot;id&quot;: 3,
      &quot;type&quot;: &quot;azimuth&quot;,
      &quot;placement&quot;: &quot;bow&quot;
    },
    {
      &quot;id&quot;: 5,
      &quot;type&quot;: &quot;azimuth&quot;,
      &quot;placement&quot;: &quot;stern&quot;
    },
    {
      &quot;id&quot;: 5,
      &quot;type&quot;: &quot;tunnel&quot;,
      &quot;placement&quot;: &quot;stern&quot;
    },
    {
      &quot;id&quot;: 6,
      &quot;type&quot;: &quot;azimuth_propulsion&quot;
    },
    {
      &quot;id&quot;: 7,
      &quot;type&quot;: &quot;azimuth_propulsion&quot;
    }
  ]
}

答案1

得分: 1

开始时,您遇到了StackOverflowException,因为您在同一个方法内部调用了相同的方法(Getthruster()),而且没有任何退出条件。

之后,您的 JSON 文件似乎是不正确的。您在那里有一个Dictionary&lt;string,thruster[]&gt;,而不是thruster[](或 List)。

您的 thruster[]List&lt;thruster&gt; 的 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&lt;string,thruster[]&gt; and not a thruster[] (Or List).

Your json for a thruster[] or List&lt;thruster&gt; should be something like:

[
  {
    &quot;id&quot;: &quot;1&quot;,
    &quot;type&quot;: &quot;t&quot;,
    &quot;placement&quot;: &quot;top&quot;
  },
  {
    &quot;id&quot;: &quot;2&quot;,
    &quot;type&quot;: &quot;t&quot;,
    &quot;placement&quot;: &quot;top&quot;
  }
]

答案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&lt;thruster&gt; test = JObject.Parse(text)[&quot;thruster&quot;].ToObject&lt;List&lt;thruster&gt;&gt;();

</details>



huangapple
  • 本文由 发表于 2023年2月8日 16:37:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75383117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定