Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

converting a dataset to JSON using json.net

Posted on 2014-12-08
12
Medium Priority
?
447 Views
Last Modified: 2014-12-11
This is a somewhat related question to my previous thread found here.

My next step that I want to accomplish is to take a dataset and convert it's contents into a JSON object, where I can then "loop" through each object and manipulate the data. being extremely new to .NET, I'm sure I'm missing a piece or two in my solution :)

I'm able to fill up my data set with nor problem, and verified this through the dataset visualizer. The code I'm using to convert my dataset is:

        private PendingOrders getOrdersToProcess(DAL_Class dbHelper)
        {
            var json = JsonConvert.SerializeObject(dbHelper.getPendingOrders(_mode));
            return JsonConvert.DeserializeObject<PendingOrders>(json);

        }

Open in new window


and my PendingOrders class:

    public class PendingOrders
    {
        public int orderID                          { get; set; }
        public int dataID                           { get; set; }
        public string recipientEmail                { get; set; }
        public string instructions                  { get; set; }
        public DateTime needBy                      { get; set; }
        public string recipientName                 { get; set; }
        public string company                       { get; set; }
        public string recipientPhone                { get; set; }
        public string address1                      { get; set; }
        public string address2                      { get; set; }
        public string City                          { get; set; }
        public string StateProv                     { get; set; }
        public string Country                       { get; set; }
        public string PostalCode                    { get; set; }
        public string CCG_Sku                       { get; set; }
        public int quantity                         { get; set; }
    }

Open in new window


the dbHelper.getPendingOrders() function fills up the dataset and returns it. if I run the following code:

var po = new PendingOrders();
po = getOrdersToProcess(dbHelper);

Open in new window


none of my data is in the po object, everything is null.

So I guess my first question is why? the JSON being returned is multiple objects (each row is it's own object). Do I need to create some kind of collection of objects here? If so, what's the best way to do that?

my second question is once I have this data converted over to some kind of (collection of) object(s), how can I loop through it and read each individual object?
0
Comment
Question by:Big Monty
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
12 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40488024
What is the return type of getPendingOrders?
0
 
LVL 33

Author Comment

by:Big Monty
ID: 40488027
It returns a data set
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40488078
Then that's your problem:  You cannot mix types--generally--when serializing. A DataSet is not a PendingOrders, so it's not going to get serizlized into one by the serializer. You'll need to perform some mapping of your DataSet into a collection of PendingOrders.
0
Give Your Engineering Team a Productivity Boost

Learn why container technology is so powerful and how it can provide your team with productivity gains and other benefits.

 
LVL 33

Author Comment

by:Big Monty
ID: 40488113
the line:

var json = JsonConvert.SerializeObject(dbHelper.getPendingOrders(_mode));

creates a string of all of the json data. I thought when I called the Deserialize function, it did the mapping for me...

Is there a better, more efficient way to do this? My end goal is to take some data from the database and put it in a JSON object so that I can use it at different points in my program
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1600 total points
ID: 40488137
creates a string of all of the json data. I thought when I called the Deserialize function
This line:

var json = JsonConvert.SerializeObject(dbHelper.getPendingOrders(_mode));

...does do that. But this line:

JsonConvert.DeserializeObject<PendingOrders>(json);

...does not do what you intend it to do. If getPendingOrders returned a PendingOrders object, then that line would work. Unfortunately, you said that it returns a DataSet. DataSet != PendingOrders. If your goal is to transmit data across the wire, then honestly I would ditch the DataSet, and just populate a collection of (what I shall call) your entity class (i.e. PendingOrders).

e.g.

public class DAL_Class 
{
    public IEnumerable<PendingOrders> getPendingOrders()
    {
        List<PendingOrders> orders = new List<PendingOrders>();
        
        using (SqlConnection con = new SqlConnection("your connection string"))
        {
            using (Sqlcommand cmd = new SqlCommand("your query", con))
            {
                try
                {
                    SqlDataReader reader;
                    
                    con.Open();
                    reader = cmd.ExecuteReader();
                    
                    while (reader.Read())
                    {
                        PendingOrders order = new PendingOrders();
                        
                        order.orderID = (int)reader["orderId column name"];
                        order.dateID = (int)reader["dateId column name"];
                        // etc.
                        
                        orders.Add(order);
                    }
                }
                catch (SqlException)
                {
                    // Handle exception
                }
            }
        }
        
        return orders;
    }
}

Open in new window



Side Note: Not that there's anything wrong with it from a functional point of view, but the convention in .NET is to use Pascal-style naming for classes, methods, and properties. Camel-casing is reserved for variable/field names. As I said, it's not broken, but don't be surprised if someone else mentions it  ; )
0
 
LVL 10

Assisted Solution

by:Walter Padrón
Walter Padrón earned 400 total points
ID: 40489288
IMHO if you are moving data between layers in your own application you better stick with a collection so you don't need to serialize and deserialize;   json is more suitable to move data between different platforms or over internet.

You can iterate over a collection with the foreach statement
0
 
LVL 33

Author Comment

by:Big Monty
ID: 40489364
@Kaufmed

thank you for that bit of code, I'm currently tying it into my project and getting it to work. I guess that was the answer to my question "what is the best collection to use when I want to take data from the database and keep in memory for later use?" what I'm ultimately trying to do is take a list of orders from the database with a status of pending, then call a 3rd party web service that returns the number of items in stock for each item ordered. as long as the number ordered is less than what's in stock, I'll call another web service and place the order

@WP

Agreed. It was a more of a learning exercise that I started off that way than anything else.
0
 
LVL 33

Author Comment

by:Big Monty
ID: 40489374
By the way, my code for calling the new getPendingOrders() function (I'm just so used to camel case!) is below. Does that look correct?

IEnumerable<PendingOrders> PendingOrders = new List<PendingOrders>();
PendingOrders = dbHelper.getPendingOrders(_mode);

Open in new window

0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 1600 total points
ID: 40489405
I don't see any errors. From a stylistic point of view:

There's no reason to initialize a new list as your first operation. The very next line assigns the result of the method call to the list, so the first initialization is really pointless. My suggestion is to collapse it into a single line:
IEnumerable<PendingOrders> PendingOrders = dbHelper.getPendingOrders(_mode);
It might be confusing to have a variable name that is the same name as a class--and in some cases it's a compiler error; surprised you're not seeing such. This is where the camel casing would come into play
IEnumerable<PendingOrders> pendingOrders = dbHelper.getPendingOrders(_mode);

Also, IMO your class name should be singular:  PendingOrder. A collection of those classes would be plural.
0
 
LVL 10

Expert Comment

by:Walter Padrón
ID: 40489411
Ahh, OK.  I suggest learn LINQ and/or EF next.

You can do something like this
var  context = new DatabaseContext();
var results = from o in context.Orders
                         where o.OrderID < 100
                         select ConvertToPendingOrders(o);

Open in new window

0
 
LVL 33

Author Closing Comment

by:Big Monty
ID: 40492457
thanks to both experts for clear concise answers and explanations!
0
 
LVL 33

Author Comment

by:Big Monty
ID: 40494393
I've added another question if you have any time to look at it:

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_28579056.html
0

Featured Post

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question