NOTE: the JSON data being passed above looks like json below when I call stringify(). The "data" object is a nested object with searchType and a list of company objects ( It contains the type of search and the companies to search for)
[Serializable]
public class SearchCriteria {
string searchType { get; set; }
List<Company> companyList { get; set; }
}
[Serializable]
public class Company {
public string CompanyId { get; set; }
public string CompanyName { get; set; }
public string Ticker { get; set; }
}
Thanks, but this doesn't really help. I poured over different docs before posting but I'm obviously not structuring my JSON correctly to be parsed into my object by the Javascript.Deserialize method. I was hoping a another set of eyes can see where I'm
going wrong by the code samples.
Dave5574
Member
30 Points
62 Posts
Pass nested JSON data from client and deserialize on server?
Jan 08, 2013 07:46 PM|LINK
Not sure if this was the best forum but I'm trying to Stringify my search criteria and pass it as a parameter to an UpdatePanel as follows:
__doPostBack('ctl00$ContentPlaceHolder1$upSearchResults', JSON.stringify(data));
NOTE: the JSON data being passed above looks like json below when I call stringify(). The "data" object is a nested object with searchType and a list of company objects ( It contains the type of search and the companies to search for)
JSON.stringify(data) ...
"{"searchType":"Company","companyList":[{"CompanyId":"36492","CompanyName":"ABC INC","Ticker":ABC},{"CompanyId":"37497","CompanyName":"ACME & CO","Ticker":ACME}]}"
However, when I recieve it on the server it looks like:
string data = Request.Params.Get("__EVENTARGUMENT");
data...
"{\"searchType\":\"Company\",\"companyList\":[{\"CompanyId\":\"36492\",\"CompanyName\":\"CATERPILLAR INC\",\"Ticker\":null},{\"CompanyId\":\"37497\",\"CompanyName\":\"DEERE & CO\",\"Ticker\":null}]}"
I can't seem to deserialize it into a C# class...neither calls below to Deserialize work....
System.Web.Script.Serialization.JavaScriptSerializer json = new System.Web.Script.Serialization.JavaScriptSerializer();
List<SearchCriteria> searchCriteria= json.Deserialize<List<SearchCriteria>>(data)
SearchCriteria criteria = json.Deserialize<SearchCriteria>(data);
The classes are below...
[Serializable]
public class SearchCriteria {
string searchType { get; set; }
List<Company> companyList { get; set; }
}
[Serializable]
public class Company {
public string CompanyId { get; set; }
public string CompanyName { get; set; }
public string Ticker { get; set; }
}
chetan.sarod...
All-Star
65819 Points
11163 Posts
Re: Pass nested JSON data from client and deserialize on server?
Jan 10, 2013 02:29 AM|LINK
Refer this
http://www.codeproject.com/Tips/79435/Deserialize-JSON-with-C
http://stackoverflow.com/questions/1207731/how-can-i-deserialize-json-to-a-simple-dictionarystring-string-in-asp-net
http://james.newtonking.com/projects/json/help/html/SerializingJSON.htm
http://geekswithblogs.net/DavidHoerster/archive/2012/01/06/json.net-and-deserializing-anonymous-types.aspx
Senior Software Engineer,
Approva Systems Pvt Ltd, Pune, India.
Dave5574
Member
30 Points
62 Posts
Re: Pass nested JSON data from client and deserialize on server?
Jan 10, 2013 03:39 PM|LINK
Thanks, but this doesn't really help. I poured over different docs before posting but I'm obviously not structuring my JSON correctly to be parsed into my object by the Javascript.Deserialize method. I was hoping a another set of eyes can see where I'm going wrong by the code samples.