Is there some way to take only the data from the dataset and ignore the start array 'result' ?
{
"result": [
"",
{
"dataset": [
{
"idSottogruppo": "7",
"Sottogruppo": "Distribuzione Ausiliaria"
}, {
"idSottogruppo": "6",
"Sottogruppo": "Distribuzione Motore"
}, {
"idSottogruppo": "8",
"Sottogruppo": "Filtri"
}, {
"idSottogruppo": "39",
"Sottogruppo": "Motore"
}
]
}
]
}
That's how I did it and it works, I just want to do less code if it's possible as all API methods have the same JSON format.
My Code:
public class OE_GetActiveSubGroupsResultDTO
{
public List<OE_GetSubActiveGroupsListDTO> Result { get; set; }
}
public class OE_GetActiveSubGroupsListDTO
{
public List<OE_GetActiveSubGroupsDTO> Dataset { get; set; }
}
public class OE_GetActiveSubGroupsDTO
{
public string idSottogruppo { get; set; }
public string Sottogruppo { get; set; }
}
public ActionResult ProcessSpareParts(CarViewModel vm)
{
OE_GetActiveItemsResultDTO activeItemsResultDeserialize = JsonConvert.DeserializeObject<OE_GetActiveItemsResultDTO>(GetActiveSubGroups);
foreach(var activeItemsResult in activeItemsResultDeserialize.Result[1].Dataset)
{
OE_GetActiveItemsDTO activeItems = new JavaScriptSerializer().Deserialize<OE_GetActiveItemsDTO>(activeItemsResult .ToString());
...
}
}
Here is the one line code
Note: this code doesn't do any validation to Input data. This code works if the format of the json doesn't change
string json = File.ReadAllText("json1.json");
var result = JObject.Parse(json)["result"][0]
.Next["dataset"]
.Select(x => new OE_GetActiveSubGroupsDTO
{
idSottogruppo = x["idSottogruppo"].ToString(),
Sottogruppo = x["Sottogruppo"].ToString()
});