Answered by:
How to convert Json format in c#.

Question
-
User2017388348 posted
How to convert Json format in c#.
WorkItemCollection results = workItemStore.Query( "Select [ID],[Work Item Type],[Assigned To],[State], [Title]" + "From WorkItems " + "Where [Work Item Type] = 'BUG'" + "Order By [State] Asc, [Changed Date] Desc");
Tuesday, February 27, 2018 6:40 AM
Answers
-
User-832373396 posted
Hi raushankarn,
How to convert JSON format in c#.public class OneClass { public int ID { get; set; } public string Work_Item_Type { get; set; } public string Assigned_To { get; set; } public string State { get; set; } public string Title { get; set; } } public string CovertToJson() { List<OneClass> theList = new List<OneClass> { new OneClass { Assigned_To= "Assigned_To1", ID=1, State= "State1",Title="title1", Work_Item_Type= "Work_Item_Type1" }, new OneClass { Assigned_To= "Assigned_To2", ID=2, State= "State2",Title="title2", Work_Item_Type= "Work_Item_Type2" }, new OneClass { Assigned_To= "Assigned_To3", ID=3, State= "State4",Title="title4", Work_Item_Type= "Work_Item_Type3" } }; JObject myJObject = JObject.FromObject(theList); string jsonStr = myJObject.ToString(); }
So in your code, it should work, but please covert WorkItemCollection to a List collection at first, please refer to
https://stackoverflow.com/questions/21822252/how-to-convert-workitemcollection-to-a-listThen
JObject myJObject = JObject.FromObject( results); string jsonStr = myJObject.ToString();
With regards, Angelina Jolie
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, February 28, 2018 8:33 AM
All replies
-
User541108374 posted
Hi,
you can use newtonsoft.json for this, likely it's already referenced in your application:
var jsonResults = JsonConvert.SerializeObject(results);
Kris.
Tuesday, February 27, 2018 6:47 AM -
User2017388348 posted
Hi ,
Thanks for the replay.
I already try same thing but i am getting error.
var str = JsonConvert.SerializeObject(results);
error is->Self referencing loop detected for property 'WorkItem' with type 'Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem'. Path 'Revisions[0]'.
Tuesday, February 27, 2018 7:03 AM -
User541108374 posted
Hi,
it seems you are referencing in 2 directions and it gets into a loop. Try to make the result instead of WorkItemCollection a custom collection (or an anonymous object) that looks like a tree object graph and serialize that.
Kris.
Tuesday, February 27, 2018 8:07 AM -
User-832373396 posted
Hi raushankarn,
How to convert JSON format in c#.public class OneClass { public int ID { get; set; } public string Work_Item_Type { get; set; } public string Assigned_To { get; set; } public string State { get; set; } public string Title { get; set; } } public string CovertToJson() { List<OneClass> theList = new List<OneClass> { new OneClass { Assigned_To= "Assigned_To1", ID=1, State= "State1",Title="title1", Work_Item_Type= "Work_Item_Type1" }, new OneClass { Assigned_To= "Assigned_To2", ID=2, State= "State2",Title="title2", Work_Item_Type= "Work_Item_Type2" }, new OneClass { Assigned_To= "Assigned_To3", ID=3, State= "State4",Title="title4", Work_Item_Type= "Work_Item_Type3" } }; JObject myJObject = JObject.FromObject(theList); string jsonStr = myJObject.ToString(); }
So in your code, it should work, but please covert WorkItemCollection to a List collection at first, please refer to
https://stackoverflow.com/questions/21822252/how-to-convert-workitemcollection-to-a-listThen
JObject myJObject = JObject.FromObject( results); string jsonStr = myJObject.ToString();
With regards, Angelina Jolie
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, February 28, 2018 8:33 AM -
User303363814 posted
Can you explain what json has to do with the problem?
results is of type WorkItemCollection, which you can iterate through
foreach(var workItem in results) { // BTW 'results' is a dreadful name, it tells you nothing foreach (var field in workItem.Fields) { Console.WriteLine($"Work item field {field.Name} has value {field.Value}"); }
Console.WriteLine(); }(I don't have TFS and have never used it, I'm just guessing from the documentation)
Thursday, March 1, 2018 1:53 AM -
User2017388348 posted
Hi Angelina,
Thank you very much for helping me. I have been used some other way but your answer is also correct.
With Regards,
Karn.
Saturday, March 3, 2018 6:29 AM