Answered by:
JArray creates empty JSON objects

Question
-
User-1590246810 posted
Dear All,
I have a web API application which brings a list of users with their expenses details. everything retrieved properly. But, it creates empty JSON object when data added into JArray.
Please have a look at my below code and response which I'm getting. Please help me on this, I'm in the critical situation.
Code :
public static JArray GetTripMembers(Dictionary<string, string> srclists, string TripId) { dynamic json = new JObject(); try { DataSet TripMem = ADFunctions.GetTripExpByMember(TripId); if (TripMem.Tables[0].Rows.Count > 0) { JArray jarrayObj = new JArray(); foreach (DataRow dr in TripMem.Tables[0].Rows) { // or cast to dynamic to dynamically add/read properties dynamic TripMember = json; string MemId = dr["trip_mem_uid"].ToString().Trim(); TripMember.EmpId = MemId; TripMember.EmpName = dr["trip_mem_name"].ToString().Trim(); TripMember.Allocated = dr["alloc_fund"].ToString() != "0" ? "True" : "False"; dynamic MemExps = new JArray(); json.Sources = MemExps; Dictionary<string, string> srcList = srclists; foreach (KeyValuePair<string, string> kvps in srcList) { DataSet srcs = ADFunctions.GetTripSources(TripId, kvps.Value, MemId); if (srcs.Tables[0].Rows.Count > 0) { foreach (DataRow drs in srcs.Tables[0].Rows) { dynamic MemExs = new JObject(); MemExs.SourceId = drs["Source_Id"].ToString(); MemExs.SourceName = drs["Source_Name"].ToString(); Double Bal = Convert.ToDouble(drs["alloc_fund"]) - Convert.ToDouble(drs["exp_amt"]); MemExs.Amount = Helper.ConvertAmount(Convert.ToDouble(drs["alloc_fund"])); MemExs.Balance = Helper.ConvertAmount(Bal); MemExps.Add(MemExs); } jarrayObj.Add(json); json = new JObject(); } } } json = JValue.Parse(jarrayObj.ToString()); } } catch(Exception ex) { Helper.addToLog("<GetTripMembers>" + ex.Message); } return json; }
Response on POSTMAN
{[
{
"EmpId": "001",
"EmpName": "Mark",
"Allocated": "True",
"Sources": [
{
"SourceId": "123",
"SourceName": "Capital",
"Amount": "5,000.00",
"Balance": "5,000.00"
},
{
"SourceId": "124",
"SourceName": "Godown",
"Amount": "7,000.00",
"Balance": "7,000.00"
}
]
},
{},
{},
{},
{},
{
"EmpId": "002",
"EmpName": "Steve",
"Allocated": "True",
"Sources": [
{
"SourceId": "123",
"SourceName": "Capital",
"Amount": "2,500.00",
"Balance": "2,500.00"
},
{
"SourceId": "124",
"SourceName": "Godown",
"Amount": "2,400.00",
"Balance": "2,400.00"
}
]
},
{},
{},
{},
{},
{
"EmpId": "003",
"EmpName": "Antony",
"Allocated": "True",
"Sources": [
{
"SourceId": "123",
"SourceName": "Capital",
"Amount": "3,200.00",
"Balance": "3,200.00"
},
{
"SourceId": "124",
"SourceName": "Godown",
"Amount": "3,300.00",
"Balance": "3,300.00"
}
]
},
{},
{},
{},
{},
{
"EmpId": "004",
"EmpName": "Stella",
"Allocated": "True",
"Sources": [
{
"SourceId": "123",
"SourceName": "Capital",
"Amount": "3,800.00",
"Balance": "3,800.00"
},
{
"SourceId": "124",
"SourceName": "Godown",
"Amount": "3,600.00",
"Balance": "3,600.00"
}
]
},
{},
{},
{},
{},
{
"EmpId": "005",
"EmpName": "Rubella",
"Allocated": "True",
"Sources": [
{
"SourceId": "123",
"SourceName": "Capital",
"Amount": "2,400.00",
"Balance": "2,400.00"
},
{
"SourceId": "124",
"SourceName": "Godown",
"Amount": "3,000.00",
"Balance": "3,000.00"
}
]
},
{},
{},
{},
{}
]}Thursday, August 17, 2017 1:57 PM
Answers
-
User-1590246810 posted
Hi All,
I resolved it. by adding below code in the end of the loop. and response got perfect.
jarrayObj.Add(json); json = new JObject();
Thanks.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, August 17, 2017 2:19 PM