Deserialize Json String in to Multiple C# Objects

Deserialize JSON string in to multiple C# objects

You were close:

var result = JsonConvert.DeserializeObject<Dictionary<string, DriverMaster>>(json)
.Select(x => x.Value)
.ToList();

Deserializing Json String into multiple Object types

As @YeldarKurmangaliyev already said, your json has two different objects, I think you can do something like this:

var j = JArray.Parse(data);
TeamsList = JsonConvert.DeserializeObject<List<Team>>(j[1].ToString());
MobileUsersList = JsonConvert.DeserializeObject<List<User>>(j[2].ToString());

How to deserialize a JSON string that has multilple objects of different type

Your json format is incorrect. change the "{"\"itemList\":[{\"id\":1,\"name\":\"Item 1 Name\"},{\"id\":2,\"name\":\"Item 2 Name\"}],"listInfo":{"info1":1,"info2":"bla"}}"

to

"{\"itemList\":[{\"id\":1,\"name\":\"Item 1 Name\"},{\"id\":2,\"name\":\"Item 2 Name\"}],\"listInfo\":{\"info1\":1,\"info2\":\"bla\"}}"

then create a class like this

public class BaseClass
{
public List<ItemClass> ItemList { get; set; }
public ListInfo ListInfo { get; set; }
}

and use JsonConvert.DeserializeObject to deserialize json to your class

string json = "{\"itemList\":[{\"id\":1,\"name\":\"Item 1 Name\"},{\"id\":2,\"name\":\"Item 2 Name\"}],\"listInfo\":{\"info1\":1,\"info2\":\"bla\"}}";
var data = JsonConvert.DeserializeObject<BaseClass>(json);

Deserialize multiple json into object c#

You said "with no success", so it's unclear if there's an error or what. But...

I think the issue is your root object. I ran your JSON through json2csharp.com and this is what it came up with:

public class PurchaseOrderLine
{
public string lineNumber { get; set; }
public string itemNumber { get; set; }
public string itemDescription { get; set; }
public double unitPrice { get; set; }
public int quantity { get; set; }
public bool isServiceBased { get; set; }
public string taxIndicator1 { get; set; }
public string taxIndicator2 { get; set; }
public string unit { get; set; }
public List<object> deliveryLines { get; set; }
public List<object> supplierItems { get; set; }
public bool isActive { get; set; }
}

public class PurchaseOrder
{
public string supplierId { get; set; }
public string currencyCode { get; set; }
public string companyId { get; set; }
public string companyName { get; set; }
public List<PurchaseOrderLine> purchaseOrderLines { get; set; }
public string orderIdentifier { get; set; }
public string supplierName { get; set; }
public string orderType { get; set; }
public bool isActive { get; set; }
}

public class RootObject
{
public List<PurchaseOrder> purchaseOrders { get; set; }
}

Try that with var root = JsonConvert.DeserializeObject<RootObject>(json);

C# Deserialize JSON array with multiple objects

Is there a simple way to to return all 5 elements in one record?

Sure -- Just put each property in a single record:

var finalRecord = new TransactionInformation
{
Type = obj.TransactionInformation.FirstOrDefault(x => !string.IsNullOrEmpty(x.Type))?.Type,
Action = obj.TransactionInformation.FirstOrDefault(x => !string.IsNullOrEmpty(x.Action))?.Action,
Owner = obj.TransactionInformation.FirstOrDefault(x => !string.IsNullOrEmpty(x.Owner))?.Owner,
BuyerBroker = obj.TransactionInformation.FirstOrDefault(x => !string.IsNullOrEmpty(x.BuyerBroker))?.BuyerBroker,
CompensationToBuyer = obj.TransactionInformation.FirstOrDefault(x => x.CompensationToBuyer.HasValue)?.CompensationToBuyer
};

That JSON data you are working with isn't in the most convenient format. In a perfect world it would look like this:

{
"Transaction Information": [{
"Type": "This is the Type",
"Action": "No",
"Owner": "Simpsons",
"Buyer/Broker": "Y",
"Compensation to Buyer": 3.0
}
]
}

Then what you were doing would have worked fine and you wouldn't have to do this last step to normalize the data.

c# system.text.json deserialize "different" object to an array of one class

You don't need the class at all.

You can just declare the property on the root object like this

public Dictionary <string, YourType> SeasonalInfoBySeasonID {get;set;} = new Dictionary <string, YourType>();

Then each key will be aa8e0d2d-d29d-4c03-84f0-b0bda96a9fe1 etc., and the objects will be the inner type.



Related Topics



Leave a reply



Submit