-1

I have a JSON string like below:

"\"{\\\"PersonNumber\\\":\\\"4537\\\",\\\"PersonName\\\":\\\"Jenny\\\"}\""

I want to convert this JSON to NameValueCollection and I have tried below codes. but I am getting an error :

string jsonString= "\"{\\\"PersonNumber\\\":\\\"4537\\\",\\\"PersonName\\\":\\\"Jenny\\\"}\""
System.Web.Script.Serialization.JavaScriptSerializer jss1;
NameValueCollection nvc1;
jss1 = new System.Web.Script.Serialization.JavaScriptSerializer();
nvc1 = new NameValueCollection();
try { nvc1 = jss1.Deserialize<NameValueCollection>(jsonString); } catch { }

Error:

Cannot convert object of type 'System.String' to type 'System.Collections.Specialized.NameValueCollection'

Where am I making a mistake?

sdm
  • 33
  • 4
  • 1
    Why are you putting all those backslashes in there? Valid JSON would be: `string jsonString = "{\"PersonNumber\":\"4537\",\"PersonName\":\"Jenny\"}";` – itsme86 Sep 16 '19 at 14:18
  • Is that really what your string looks like? It appears to have been double-serialized, and if so, you will need to deserialize it twice, once to a `string` and then again to your data model. – dbc Sep 16 '19 at 19:56
  • Assuming your JSON is not double-serialized see [How to convert json to NameValueCollection](https://stackoverflow.com/q/11398882) (for `JavaScriptSerializer`) and [how to convert NameValueCollection to JSON string?](https://stackoverflow.com/q/7003740) and [ArgumentNullException in Json.NET 6.0.7 when deserializing into NameValueCollection](https://stackoverflow.com/q/27828350). – dbc Sep 16 '19 at 20:03

2 Answers2

0

If you specifically need a NameValueCollection then the simplest way is simply use DeserializeObject and then add the items to a NameValueCollection like so:

string jsonString= "\"{\\\"PersonNumber\\\":\\\"4537\\\",\\\"PersonName\\\":\\\"Jenny\\\"}\""

System.Web.Script.Serialization.JavaScriptSerializer jss1;
NameValueCollection nvc1;
IDictionary<string, object> dict;

jss1 = new System.Web.Script.Serialization.JavaScriptSerializer();
nvc1 = new NameValueCollection();
dict = jss1.DeserializeObject(jsonString);

foreach (var kvPair in dict)
{
  nvc1.Add(kvPair.Key, kvPair.Value);
}
Dan
  • 901
  • 11
  • 25
0

Given a pretty simple extension method on Dictionary<string,T>:

public static class DictionaryExtensions
{
    public static NameValueCollection ToNameValueCollection<T>(this IDictionary<string, T> dictionary)
    {
        var collection = new NameValueCollection();
        foreach(var pair in dictionary)
            collection.Add(pair.Key, pair.Value?.ToString());
        return collection;
    }
}

This is as easy as deserializing your json to a dictionary and using the extension method:

var nvc = JsonConvert.DeserializeObject<Dictionary<string,string>>(jsonString)
                     .ToNameValueCollection();

Note: Uses Newtonsoft.Json, but any deserializer should be able to deserialize your jsonString directly to Dictionary<string,string>.

Live example: https://dotnetfiddle.net/vxqumd

Jamiec
  • 133,658
  • 13
  • 134
  • 193