将JSON项复制到另一个列表 [英] Copy JSON item to another list

查看:91
本文介绍了将JSON项复制到另一个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将JSON项目复制到另一个列表。我在C#.Net中的JSON RootObject代码如下:



I am trying to copy JSON item to another list. My code for the JSON RootObject in C#.Net is as follows:

public class RootObject
    {
        public string epc { get; set; }
        public string tagId { get; set; }
        public object jobId { get; set; }
        public string fromZone { get; set; }
        public object fromFloor { get; set; }
        public string toZone { get; set; }
        public object toFloor { get; set; }
        public object fromFacility { get; set; }
        public string toFacility { get; set; }
        public object fromX { get; set; }
        public object fromY { get; set; }
        public object toX { get; set; }
        public object toY { get; set; }
        public DateTime observationTime { get; set; }
    }





我尝试过:



这是我使用newtonsoft将数据转换为JSON的代码





What I have tried:

And this is my code to convert data into JSON using newtonsoft

var settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
var jItem = JsonConvert.DeserializeObject<RootObject>(messege, settings);





现在我的目标是创建数据类型RootObject的列表并将JSON元素复制到列表中,以便我可以使用相同的列表将数据保存到数据库中。我正在使用MS SQL Server。



实际上,我每隔15秒从AMQP队列获取一堆数据。此数据采用JSON字符串的形式。所以我想每15秒将数据堆叠到一个列表中,然后将相同的列表传递给数据库以存储数据。所以我的逻辑是将JSON字符串复制到列表中并将相同的列表传递给数据库。



Now my objective is to create a list of datatype RootObject and copy the JSON elements into a list so that I can use the same list to save the data into database. I am using MS SQL Server.

Actually, I am getting a bunch of data after every 15 seconds of interval from AMQP queue. This data is in the form of JSON string. So I want to stack up the data into a list every 15 seconds and then pass the same list to the database to store the data. So my logic is to copy the JSON string into list and pass the same list to the database.

推荐答案

Quote:

@Karthik:问题是我想将JSON对象复制到一个新列表中。

@Karthik: Issue is I want to copy the JSON object into a new list.



试试这个


try this

string[] messages = GetMessages(); // get the messages
            List<RootObject> list = new List<RootObject>();
            var settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
            foreach (string msg in messages)
            {
                RootObject obj = JsonConvert.DeserializeObject<RootObject>(message, settings);
                list.Add(obj);
            }


这篇关于将JSON项复制到另一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆