I encountered an issue which is I want to get the data from innermost json. Eg, I input Swimming then I will get student_ID : 0001 and Name : Jack. Something like this but I don't want to use Newtonsoft or any other dll. My current code is shown below, I able to get data for the outmost which is Name and Birthday but not embedded. My code and json are below
{
"Name":"Jack",
"birthday":"11-Jan-2022",
"embedded":{
"OtherInfo":[
{
"student_ID":"0001",
"Batch":"2022-02",
"Hobby":{
"Sport":"Swimming",
"Arcade":"Fencing"
}
}
]
}
}
public class Hobby
{
public string Sport { get; set; }
public string Arcade { get; set; }
}
public class OtherInfo
{
public string student_ID { get; set; }
public string Batch { get; set; }
public Hobby Hobby { get; set; }
}
public class Embedded
{
public List<OtherInfo> OtherInfo { get; set; }
}
public class Root
{
public string Name { get; set; }
public string birthday { get; set; }
public Embedded embedded { get; set; }
}
var jsonObj = new JavaScriptSerializer().Deserialize<Root>(ApiCreateJsonData);
string testtsett = "";
foreach (var item in jsonObj.Name)
{
testtsett = item.ToString();
}
Label1.Text = testtsett;
CodePudding user response:
It is as simple as:
foreach (var item in jsonObj.embedded.OtherInfo)
{
testtsett = item.student_ID;
testtsett = item.Hobby.Arcade;
testtsett = item.Hobby.Sport;
testtsett = item.Batch;
}
You need to loop over your List<OtherInfo>
to get your embedded
values: