Answered by:
Json Data not returning ID

Question
-
User-284642143 posted
I have a Json file which has an Id and Title.
I retrieve the data by
using (StreamReader r = new StreamReader(ConfigurationManager.AppSettings["Path"])) { string json = r.ReadToEnd(); DeviceIssues.RootObject items = JsonConvert.DeserializeObject<DeviceIssues.RootObject>(json); return items; }
Within the json variable i can see all the items are populated with ID and Title but once it gets into the items variable it only holds the title and no ID - Is it possible to have the ID included?
Tuesday, January 22, 2019 9:13 PM
Answers
-
User753101303 posted
Hi,
What if you change "-id" to "id" and try again ?
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, January 23, 2019 11:43 AM
All replies
-
User475983607 posted
We'll need to see the file and DeviceIssues.RootObject in order to reproduce the issue
Otherwise, the NewtonSoft docs cover reading a file and converting to an object.
https://www.newtonsoft.com/json/help/html/DeserializeWithJsonSerializerFromFile.htm
Tuesday, January 22, 2019 9:57 PM -
User-893317190 posted
Hi EssCee,
Not sure about the structure of your json file and your model RootObject, I have made a small sample to show how to convert json string to .net object.
Below is my json file.
[ { "Id": 1, "title": "title1" }, { "Id": 2, "title": "title2" }, { "Id": 3, "title": "title3" }, { "Id": 4, "title": "title4" }, { "Id": 5, "title": "title5" } ]
My code behind.
public partial class ReadJsonFile : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string json = File.ReadAllText(Server.MapPath("/jsonDemo/json.json")); List<RootObject> root = JsonConvert.DeserializeObject<List<RootObject>>(json); GridView1.DataSource = root; GridView1.DataBind(); } } public class RootObject { public int id { get; set; } public string title { get; set; } }
And my gridview.
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
The result.
If it is not your case , please post your json file and RootObject.
Best regards,
Ackerly Xu
Wednesday, January 23, 2019 3:10 AM -
User-284642143 posted
Thanks Guys
"devices": { "phone": { "issues": { "issue": [ { "-id": "1", "title": "One" }, { "-id": "2", "title": "Two" }, ] } }, "computer": { "issues": { "issue": [ { "-id": "1", "title": "Screen" }, public class Devices { public Phone phone { get; set; } public Computer computer { get; set; } } public class RootObject { public Devices devices { get; set; } }
To filter the data by device i have
private DeviceIssues.Phone phoneIssues => RootObject.devices.phone;
I iterate through the list and bring back the values
public List<Class.Issues> PhoneIssuesList() { List<Issues> PhoneIssues = new List<Issues>(); foreach (DeviceIssues.Issue s in phoneIssues.issues.issue) { PhoneIssues.Add(new Issues(Convert.ToInt32(s.id), s.title)); } return PhoneIssues; }
All works but as i mentioned the ID is missing but the title returns. I generated the RootObject class by entering the json into an online tool which returned the C# class
Wednesday, January 23, 2019 11:01 AM -
User753101303 posted
Hi,
What if you change "-id" to "id" and try again ?
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, January 23, 2019 11:43 AM -
User-284642143 posted
Well done!! the most basic thing i missed. Cheers
Wednesday, January 23, 2019 12:10 PM