Reputation: 29444
I am new to JSON.NET and not sure how to do this.
I have the CartItem
class, and I need to implement
GetAllCartItemsFromArbitraryJson(string jsonStr)
, as follows:
class CartItem {
public int Id;
public int Qty;
}
List<CartItem> items = GetAllCartItemsFromArbitraryJson(jsonStr);
All I know is jsonStr
contains one or more cartitems
somewhere (I don't know how deep), eg.
{
... : {
"cartitems": [{
"id": "1",
"qty": "1"
},{
"id": "2",
"qty": "5"
}
]
},
... : {
... : {
...,
"cartitems": [{
"id": "10",
"qty": "2"
}
]
}
}
}
This function needs to collect all the cartitems
and put it in List<CartItem>
List<CartItem> GetAllCartItemsFromArbitraryJson(string jsonStr) {
JObject json = JObject.Parse(jsonStr);
// then what...?
}
So the List<CartItem>
will contains:
Id Qty
1 1
2 5
10 2
How would you do it in C#?
Upvotes: 1
Views: 1283
Reputation: 29444
This is a solution that works:
List<CartItem> GetAllCartItemsFromArbitraryJson(string jsonStr) {
JObject json = JObject.Parse(jsonStr);
return json.Descendants().OfType<JProperty>() // so we can filter by p.Name below
.Where(p => p.Name == "cartitems")
.SelectMany(p => p.Value) // selecting the combined array (joined into a single array)
.Select(item => new CartItem {
Id = (int)item["id"],
Qty = (int)item["qty"]
}).ToList();
}
Hope it helps someone.
I am new to JSON though, got that by trial & error. So let me know if it can be improved :)
Upvotes: 4
Reputation: 2896
Parse to dynamic and iterate through the items.
public List<CartItem> PostAllCartItemsFromArbitraryJson(string jsonStr)
{
List<CartItem> AllCartItems = new List<CartItem>();
try
{
dynamic BaseJson = JObject.Parse(jsonStr.ToLower());
CheckForCarts(AllCartItems, BaseJson);
}
catch (Exception Error)
{
}
return AllCartItems;
}
private void CheckForCarts(List<CartItem> AllCartItems, dynamic BaseJson)
{
foreach (dynamic InnerJson in BaseJson)
{
if (InnerJson.Name == "cartitems")
{//Assuming this is an [] of cart items
foreach (dynamic NextCart in InnerJson.Value)
{
try
{
CartItem FoundCart = new CartItem();
FoundCart.Id = NextCart.id;
FoundCart.Qty = NextCart.qty;
AllCartItems.Add(FoundCart);
}
catch (Exception Error)
{
}
}
}
else if (InnerJson.Value is JObject)
{
CheckForCarts(AllCartItems, InnerJson.Value);
}
}
}
public class CartItem
{
public int Id;
public int Qty;
}
For your sample input this generates:
[{"Id":1,"Qty":1},{"Id":2,"Qty":5},{"Id":10,"Qty":2}]
Upvotes: 1