I have a Generic List object which hold below table as its own value.
CountryID | StateID | StateName
--------------------------------
1 | 1 | Alabama
1 | 2 | California
1 | 3 | Florida
1 | 4 | Hawaii
2 | 5 | London
2 | 6 | Oxford
I would like to create JSON string according to that List object. The JSON format which i would like to get is like below.
{
1: { '1': 'Alabama', '2': 'California', '3': 'Florida', '4': 'Hawaii' },
2: { '5': 'London', '6': 'Oxford' }
};
I used below class to generate JSON object.
public static class JSONHelper
{
public static string ToJSON(this object obj)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(obj);
}
public static string ToJSON(this object obj, int recursionDepth)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.RecursionLimit = recursionDepth;
return serializer.Serialize(obj);
}
}
But , the actual output that I got when I finished calling ToJSON method is like below.
{
[
{"CountryID":1,"StateID":1,"StateName":"Alabama"},
{"CountryID":1,"StateID":2,"StateName":"California"},
{"CountryID":1,"StateID":3,"StateName":"Florida"},
{"CountryID":1,"StateID":4,"StateName":"Hawaii"},
{"CountryID":2,"StateID":5,"StateName":"London"},
{"CountryID":2,"StateID":6,"StateName":"Oxford"}
]
}
So , Could anyone please give me suggestion how could I make JSON string format as I want ?
Every suggestion will be appreciated.
var _CountryNState = objRepository.CountryNState_Select_Lookup().CountryNState; /// which is generic List object _CountryNState.ToJSON();