C#处理JSON文件中的多个元素

woobm2wo  于 5个月前  发布在  C#
关注(0)|答案(3)|浏览(73)

我有一个JSON文件,结构如下:

{
    "name": "ML4 093 Historical",
    "unit": "pounds",
    "data":[
        {
            "date": "1985-12-01",
            "value": "250"
        },
        {
            "date": "1985-11-01",
            "value": "237"
        },
        {
            "date": "1985-10-01",
            "value": "286"
        }
    ]
}

字符串
长话短说,我需要获取对应于特定日期的值(例如,250表示1985-12-01,237表示1985-11-01)。
我知道如何获取单个元素的信息(例如,获取单位的磅数),但我不知道在这种情况下如何去做。
这就是我要获取单个元素的信息:

using System.Text.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace test
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
             Label unitname = label3;

            string QUERY_URL = "JSON URL HERE";
                Uri queryUri = new Uri(QUERY_URL);

            using (System.Net.WebClient client = new System.Net.WebClient())
            {
                  var json_data = client.DownloadString(queryUri);

                  var jsonDocument = JsonDocument.Parse(json_data);

                  var rootElement = jsonDocument.RootElement;

                  var unitElement = rootElement.GetProperty("unit");

                  var unit = unitElement.GetString();

                  unitname.Text = unit;
            }

        }
    }
}


很明显,JSON URL在这里被代码中的实际URL取代了-我只是不能公开分享它。

13z8s7eq

13z8s7eq1#

你可以创建一个表示这个JSON对象的类(记住Data应该是一个完全独立的对象,在父对象中),像这样:

public class DataObject
    {
        public string date { get; set; }
        public string value { get; set; }
    }

    public class FormObject
    {
        public string name { get; set; }
        public string unit { get; set; }
        public List<Data> data { get; set; }
    }

字符串
然后要检索该值,示例如下:

var rootElement = (DataObject)jsonDocument.RootElement;  //there are other better ways to do this - you might want to take a look at Automapper library
var unitElement = rootElement.Data[1].Value  //to get the first data value element.

tjrkku2a

tjrkku2a2#

您可以创建一个模型类
DataModel.cs

using System.Collections.Generic;

namespace MyFoodBag.Forecast.Presentation.Forecaster.Models
{
    public class DataModel
    {
        public string Name { get; set; }
        public string Unit { get; set; }
        public List<Data> Data { get; set; }
    }

    public class Data
    {
        public string Date { get; set; }
        public string Value { get; set; }
    }
}

字符串
在那之后,只需对代码进行一点修改就可以了。

using System.Text.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace test
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
             Label unitname = label3;

            string QUERY_URL = "JSON URL HERE";
            Uri queryUri = new Uri(QUERY_URL);

            using (System.Net.WebClient client = new System.Net.WebClient())
            {
                  var json_data = client.DownloadString(queryUri);
                  // map json to model object
                  var dataModel= JsonConvert.DeserializeObject<DataModel>(json_data);

                  var valueForDate = dataModel.Data.Where(x => x.Date == <whateverdate>).Select(v => v.Value);
            }

        }
    }
}

sauutmhj

sauutmhj3#

尝试以下操作

using (SqlConnection conn = new SqlConnection(strConnection))
 {
     conn.Open();
    //unit and name
    //data colletion with bellow code
    foreach (<listname> objName in requestObj.CollectionName)
    {
        SqlDataAdapter adapter = new SqlDataAdapter();
        
        using (adapter.SelectCommand = new SqlCommand(spName, conn))
        {
            adapter.SelectCommand.CommandTimeout = 0;
            adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
    
            PropertyInfo[] props = requestObj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
       
            adapter.SelectCommand.Parameters.Add(new SqlParameter("ParameterName", CollectionName.object));
    
            adapter.Fill(ds);
        }
    }
}

字符串

相关问题