Matt Altepeter
Matt Altepeter

Reputation: 956

JSON formatting issue

I am using JSON.NET to create a JSON string. Basically, I have three List objects that contain information I retrieve from a database. Each list contains information about companies at a certain level of partnership (gold, silver, and bronze). I need my web method to return this JSON string where each partnership level is a sub level of the string, with each of those companies and their information listed within that sub level. I tried using JsonConvert.SerializeObject() for each of my lists. The issue with this is that I need to have sub levels in my JSON string, as seen below:

{
  "gold": {
            //each gold level company
            { "name": name, "logo": logo, ... },
            { "name": name, "logo": logo, ... },
            ...
          },
 "silver": { ... },
 "bronze": { ... }
}

Can someone give me some tips on the best way to achieve this using JSON.NET?

Upvotes: 0

Views: 76

Answers (1)

L.B
L.B

Reputation: 116178

var json  = JsonConvert.SerializeObject(
                new { gold = goldList, silver = silverList, bronze = bronzeList });

where goldList, silverList and bronzeList are your lists.

Upvotes: 2

Related Questions