David Rudlin
asked on
How to extract parent values and all child values from deserialized JSON, one record per child value, to a List<T>?
How do I extract parent values and all child values from deserialized JSON, one record per child value, to a List<T>?
Sample JSON: json_sample.txt
So from the attached sample JSON we wish to add the following records to the list:
Record 1: aen: Y456789, studentID: 1016, need: MMM, rank: 1
Record 2: aen: Y456789, studentID: 1016, need: PPP, rank: 2
All records with "needs : null" to be ignored and all "need": "Not Specified" to be ignored. Sample code below:
CTFNeedDataBatch =
(from people in myJSON.content
select new NeedCTF
{
studID = people.StudentBasic.studen tId.ToStri ng(),
AEN= people.StudentBasic.aen.To String(),
Code = (people.StudentSensitive.n eeds == null ? people.StudentSensitive.ne eds[0].nee d.ToString () : ""),
Priority = (people.StudentSensitive.n eeds == null ? people.StudentSensitive.ne eds[0].ran k.ToString () : ""),
}).Where(w => w.Code != "").ToList();
The problem with this code is we are not getting any values from the "needs" element and even if we were, I doubt we would be getting more than one record from an element that has more than one need.
Sample JSON: json_sample.txt
So from the attached sample JSON we wish to add the following records to the list:
Record 1: aen: Y456789, studentID: 1016, need: MMM, rank: 1
Record 2: aen: Y456789, studentID: 1016, need: PPP, rank: 2
All records with "needs : null" to be ignored and all "need": "Not Specified" to be ignored. Sample code below:
CTFNeedDataBatch =
(from people in myJSON.content
select new NeedCTF
{
studID = people.StudentBasic.studen
AEN= people.StudentBasic.aen.To
Code = (people.StudentSensitive.n
Priority = (people.StudentSensitive.n
}).Where(w => w.Code != "").ToList();
The problem with this code is we are not getting any values from the "needs" element and even if we were, I doubt we would be getting more than one record from an element that has more than one need.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I am writing from my phone and i do not have a pc near to test anything, but i do see an issue in your code here:
Open in new window
Esdentially you say, "if the needs is null then get the first need to string, else get an empty string. You should have it like this :
Open in new window
Giannis