如何从.json文件中获取数据,仅使用其中的一些项目,并使用该数据填充组合框?

时间:2021-08-08 06:01:10

What I'm looking to do is take my .json file, grab only certain items from that file, and then populate the items in a combobox with that filtered data (using Newtonsoft / Json.net). I'll give you an example:

我想要做的是获取我的.json文件,仅从该文件中获取某些项目,然后使用该过滤后的数据填充组合框中的项目(使用Newtonsoft / Json.net)。我举个例子:

(some of the) JSON File Data:

(一些)JSON文件数据:

[
  {
    "name": "Kerbol",
    "radius": 261600000,
    "mass": 1.7565670e+28
  },
  {
    "name": "Moho",
    "radius": 250000,
    "mass": 2.5263617e+21
  },
  {
    "name": "Eve",
    "radius": 700000,
    "mass": 1.2244127e+23
  },
]

This isn't all of the data, just some of it. It's some info on the planets in the game "Kerbal Space Program". The only thing I'm interested in (right now) is only grabbing every "name" item in the .json file. Then I want to populate the items property in a combobox with all of those names (on each line).

这不是所有数据,只是其中的一部分。这是游戏“Kerbal Space Program”中行星的一些信息。我唯一感兴趣的是(现在)只抓取.json文件中的每个“name”项。然后我想在一个组合框中填充所有这些名称的items属性(在每一行)。

I've tried a lot of other code to filter it out, but I don't understand enough to rewrite it to work how I need.

我已经尝试了很多其他代码来过滤掉它,但我不太了解它可以重写它以满足我的需要。

EDIT: I want to do more with this .json data later on, but I'm just trying to do this one step at a time.

编辑:我想稍后用这个.json数据做更多的事情,但我只是想一步一步地做这个。

EDIT2: This is for Windows Forms.

EDIT2:这适用于Windows窗体。

2 个解决方案

#1


0  

Here is a wpf demo, i have test it on my computer, you can try.

这是一个wpf演示,我在我的电脑上测试过,你可以试试。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        var data = MyModel.getTestData();
        var dataStr = JsonConvert.SerializeObject(data, Formatting.Indented);
        using (var writer = new StreamWriter(@"d:\data.json", false))
        {
            writer.Write(dataStr);
        }
        using (var reader = new StreamReader(@"d:\data.json"))
        {
            var recs = JsonConvert.DeserializeObject<List<MyModel>>(reader.ReadToEnd());
            cbx.ItemsSource = recs;//cbx is ComboBox
            cbx.DisplayMemberPath = "name";
            cbx.SelectedIndex = 0;
        }
    }
}

public class MyModel
{
    public string name { get; set; }
    public double radius { get; set; }
    public double  mass  { get; set; }

    public static List<MyModel> getTestData()
    {
        return new List<MyModel>
        {
            new MyModel {name = "x1", radius = 1, mass = 1},
            new MyModel {name = "x2", radius = 2, mass = 2},
            new MyModel {name = "x3", radius = 3, mass = 3},
            new MyModel {name = "x4", radius = 4, mass = 4},
        };
    }
}

#2


0  

I managed to find some people on Discord who I could talk to, so that helped a lot better. Pretty much every piece of code I ever saw trying to look this up was unhelpful for me as I didn't understand it enough to salvage it. So here's what it eventually ended up being:

我设法在Discord上找到了一些我可以与之交谈的人,所以这有助于更好。几乎每一段我试过看过的代码都对我没有帮助,因为我不太了解它以挽救它。所以这就是它最终的结果:

celestialbodydata.cs

celestialbodydata.cs

using System;

namespace KMAP
{
    class CelestialBodyData
    {
        public string name { get; protected set; }
        public double radius { get; protected set; }
        public double mass { get; protected set; }

        public CelestialBodyData(string name, double radius, double mass)
        {
            this.name = name;
            this.radius = radius;
            this.mass = mass;
        }
    }
}

Main Form.cs:

主表格:

private void Form1_Load(object sender, EventArgs e)
{
    SemMajAxTab_InputBodyRadius_ComboBox.Items.Clear();
    OPTab_InputBodyMass_ComboBox.Items.Clear();
    OPTab_InputBodyMass2_ComboBox.Items.Clear();
    OPTab_InputBodyRadius_ComboBox.Items.Clear();
    HohTab_InputBodyMass_ComboBox1.Items.Clear();
    HohTab_InputBodyRadius_ComboBox1.Items.Clear();
    HohTab_InputBodyMass_ComboBox2.Items.Clear();
    HohTab_InputBodyRadius_ComboBox4.Items.Clear();

    CelestialBodyData[] celestialbodydata_Array = JsonConvert.DeserializeObject<CelestialBodyData[]>(File.ReadAllText(@"C:\Users\Anase\Desktop\Visual C\KMAP\KMAP\bin\Release\celestialbodydata.json"));

    string[] namesarray = new string[celestialbodydata_Array.Length];

    for (int i = 0; i < celestialbodydata_Array.Length; i++)
    {
        namesarray[i] = (celestialbodydata_Array[i].name).ToString();
    }

    SemMajAxTab_InputBodyRadius_ComboBox.Items.AddRange(namesarray);
    OPTab_InputBodyMass_ComboBox.Items.AddRange(namesarray);
    OPTab_InputBodyMass2_ComboBox.Items.AddRange(namesarray);
    OPTab_InputBodyRadius_ComboBox.Items.AddRange(namesarray);
    HohTab_InputBodyMass_ComboBox1.Items.AddRange(namesarray);
    HohTab_InputBodyRadius_ComboBox1.Items.AddRange(namesarray);
    HohTab_InputBodyMass_ComboBox2.Items.AddRange(namesarray);
    HohTab_InputBodyRadius_ComboBox4.Items.AddRange(namesarray);
}

It's a lot simpler than a lot of other stuff I've seen. The hard part is going to be getting it so that when you click on an item in the combobox, it searches the array for that name and then grabs the adjacent mass or radius value and replaces the combobox's text with that. I'm new to loops and arrays.

它比我见过的许多其他东西简单得多。困难的部分是获得它,以便当您单击组合框中的项目时,它会在数组中搜索该名称,然后抓取相邻的质量或半径值,并用该替换组合框的文本。我是循环和数组的新手。

#1


0  

Here is a wpf demo, i have test it on my computer, you can try.

这是一个wpf演示,我在我的电脑上测试过,你可以试试。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        var data = MyModel.getTestData();
        var dataStr = JsonConvert.SerializeObject(data, Formatting.Indented);
        using (var writer = new StreamWriter(@"d:\data.json", false))
        {
            writer.Write(dataStr);
        }
        using (var reader = new StreamReader(@"d:\data.json"))
        {
            var recs = JsonConvert.DeserializeObject<List<MyModel>>(reader.ReadToEnd());
            cbx.ItemsSource = recs;//cbx is ComboBox
            cbx.DisplayMemberPath = "name";
            cbx.SelectedIndex = 0;
        }
    }
}

public class MyModel
{
    public string name { get; set; }
    public double radius { get; set; }
    public double  mass  { get; set; }

    public static List<MyModel> getTestData()
    {
        return new List<MyModel>
        {
            new MyModel {name = "x1", radius = 1, mass = 1},
            new MyModel {name = "x2", radius = 2, mass = 2},
            new MyModel {name = "x3", radius = 3, mass = 3},
            new MyModel {name = "x4", radius = 4, mass = 4},
        };
    }
}

#2


0  

I managed to find some people on Discord who I could talk to, so that helped a lot better. Pretty much every piece of code I ever saw trying to look this up was unhelpful for me as I didn't understand it enough to salvage it. So here's what it eventually ended up being:

我设法在Discord上找到了一些我可以与之交谈的人,所以这有助于更好。几乎每一段我试过看过的代码都对我没有帮助,因为我不太了解它以挽救它。所以这就是它最终的结果:

celestialbodydata.cs

celestialbodydata.cs

using System;

namespace KMAP
{
    class CelestialBodyData
    {
        public string name { get; protected set; }
        public double radius { get; protected set; }
        public double mass { get; protected set; }

        public CelestialBodyData(string name, double radius, double mass)
        {
            this.name = name;
            this.radius = radius;
            this.mass = mass;
        }
    }
}

Main Form.cs:

主表格:

private void Form1_Load(object sender, EventArgs e)
{
    SemMajAxTab_InputBodyRadius_ComboBox.Items.Clear();
    OPTab_InputBodyMass_ComboBox.Items.Clear();
    OPTab_InputBodyMass2_ComboBox.Items.Clear();
    OPTab_InputBodyRadius_ComboBox.Items.Clear();
    HohTab_InputBodyMass_ComboBox1.Items.Clear();
    HohTab_InputBodyRadius_ComboBox1.Items.Clear();
    HohTab_InputBodyMass_ComboBox2.Items.Clear();
    HohTab_InputBodyRadius_ComboBox4.Items.Clear();

    CelestialBodyData[] celestialbodydata_Array = JsonConvert.DeserializeObject<CelestialBodyData[]>(File.ReadAllText(@"C:\Users\Anase\Desktop\Visual C\KMAP\KMAP\bin\Release\celestialbodydata.json"));

    string[] namesarray = new string[celestialbodydata_Array.Length];

    for (int i = 0; i < celestialbodydata_Array.Length; i++)
    {
        namesarray[i] = (celestialbodydata_Array[i].name).ToString();
    }

    SemMajAxTab_InputBodyRadius_ComboBox.Items.AddRange(namesarray);
    OPTab_InputBodyMass_ComboBox.Items.AddRange(namesarray);
    OPTab_InputBodyMass2_ComboBox.Items.AddRange(namesarray);
    OPTab_InputBodyRadius_ComboBox.Items.AddRange(namesarray);
    HohTab_InputBodyMass_ComboBox1.Items.AddRange(namesarray);
    HohTab_InputBodyRadius_ComboBox1.Items.AddRange(namesarray);
    HohTab_InputBodyMass_ComboBox2.Items.AddRange(namesarray);
    HohTab_InputBodyRadius_ComboBox4.Items.AddRange(namesarray);
}

It's a lot simpler than a lot of other stuff I've seen. The hard part is going to be getting it so that when you click on an item in the combobox, it searches the array for that name and then grabs the adjacent mass or radius value and replaces the combobox's text with that. I'm new to loops and arrays.

它比我见过的许多其他东西简单得多。困难的部分是获得它,以便当您单击组合框中的项目时,它会在数组中搜索该名称,然后抓取相邻的质量或半径值,并用该替换组合框的文本。我是循环和数组的新手。