Big Monty
asked on
converting a dataset to JSON using json.net
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:
and my PendingOrders class:
the dbHelper.getPendingOrders( ) function fills up the dataset and returns it. if I run the following code:
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?
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);
}
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; }
}
the dbHelper.getPendingOrders(
var po = new PendingOrders();
po = getOrdersToProcess(dbHelper);
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?
What is the return type of getPendingOrders?
ASKER
It returns a data set
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.
ASKER
the line:
var json = JsonConvert.SerializeObjec t(dbHelper .getPendin gOrders(_m ode));
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
var json = JsonConvert.SerializeObjec
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@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.
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.
ASKER
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);
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Ahh, OK. I suggest learn LINQ and/or EF next.
You can do something like this
You can do something like this
var context = new DatabaseContext();
var results = from o in context.Orders
where o.OrderID < 100
select ConvertToPendingOrders(o);
ASKER
thanks to both experts for clear concise answers and explanations!
ASKER
I've added another question if you have any time to look at it:
https://www.experts-exchange.com/questions/28579056/trying-to-read-in-a-json-response-from-a-web-service.html
https://www.experts-exchange.com/questions/28579056/trying-to-read-in-a-json-response-from-a-web-service.html