Click here to Skip to main content
16,020,669 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to copy JSON item to another list. My code for the JSON RootObject in C#.Net is as follows:

C#
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; }
    }


What I have tried:

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

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


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.
Posted
Updated 11-Jan-18 4:23am
Comments
Karthik_Mahalingam 11-Jan-18 9:23am    
so what is the issue?
add the json object to RootObject List
webmail123 11-Jan-18 9:31am    
@Karthik: Issue is I want to copy the JSON object into a new list.

1 solution

Quote:
@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);
            }
 
Share this answer
 
v2
Comments
webmail123 11-Jan-18 10:35am    
@Karthik. Thank you very much for the answer. It worked out partially. Now this is what happening. My message string is "{"epc":"85100018","tagId":"","jobId":null,"fromZone":"FACILITY","fromFloor":null,"toZone":"FACILITY","toFloor":null,"fromFacility":"Labs","toFacility":"Labs","fromX":1.90,"fromY":-5.71,"toX":2.42,"toY":-5.44,"observationTime":"2018-01-11T15:31:30.092Z"}"

But when I execute the code mentioned above I am getting duplicate records in a List. At the end of the step, my list count was 253. And all items are same in List.
Karthik_Mahalingam 11-Jan-18 10:45am    
webmail123 11-Jan-18 11:27am    
@Karthik. Thank you very much Karthik. Actually, I excluded loop and it worked.
Karthik_Mahalingam 11-Jan-18 11:32am    
welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900