I tried:
NameValueCollection Data = new NameValueCollection();
Data.Add("foo","baa");
string json = new JavaScriptSerializer().Serialize(Data);
it returns: ["foo"] I expected {"foo" : "baa"}
How do I to do this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
NameValueCollectionisn’t an IDictionary, so theJavaScriptSerializercannot serialize it as you expect directly. You’ll need to first convert it into a dictionary, then serialize it.Update: following questions regarding multiple values per key, the call to
nvc[key]will simply return them separated by a comma, which may be ok. If not, one can always callGetValuesand decide what to do with the values appropriately. Updated the code below to show one possible way.