How to convert json to NameValueCollection

How could you convert a JSON string to C # NameValueCollectionsimply, preferably without using a third-party parser?

+5
source share
3 answers

I'm not sure why everyone still recommends JSON.NET for JSON deserialization. I wrote a post on how to deserialize JSON in C # .

In short, it looks like this:

using System.Web.Script.Serialization;

var jss = new JavaScriptSerializer();
var dict = jss.Deserialize<Dictionary<string, string>>(jsonText);

NameValueCollection nvc = null;
if (dict != null) {
  nvc = new NameValueCollection(dict.Count);
  foreach (var k in dict) {
    nvc.Add(k.Key, k.Value);
  }
}
                    }
var json = jss.Serialize(dict);
Console.WriteLine(json);

Be sure to add a link to System.Web.Extensions.dll.

Note: I usually deserialize up dynamic, so I assume that NameValueCollectionwill work. However, I did not check if this was really happening.

+10
source

EDIT

Pure.net- : JavaScriptSerializer - JSON


Json.NET

string jsonstring = @"{""keyabc"":""valueabc"",""keyxyz"":""valuexyz""}";

Dictionary<string, string> values = 
   JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonstring);

@jon : .Net Linq to JSON Newtonsoft JSON

+3

JSON , ( JSON.NET, JSON ).

:

var json = "{\"status\":\"paid\",\"date\":\"2019-10-09T17:30:51.479Z\",\"customer\":{\"id\":123456789,\"country\":\"br\",\"name\":\"Thomas Vilhena\",\"phone_numbers\":[\"+5511987654321\"],\"documents\":[{\"id\":\"doc_id\"}]}}";

var dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);            
var nvc = new NameValueCollection(dict.Count);
AddKeyValuePairs(nvc, dict);

Console.WriteLine(nvc["status"]);
Console.WriteLine(nvc["date"]);
Console.WriteLine(nvc["customer[phone_numbers][0]"]);
Console.WriteLine(nvc["customer[id]"]);
Console.WriteLine(nvc["customer[documents][0][id]"]);

:

paid
09.10.2019 17:30
+5511987654321
123456789
doc_id

:

private static void AddKeyValuePairs(
    NameValueCollection nvc,
    Dictionary<string, object> dict,
    string prefix = null)
{
    foreach (var k in dict)
    {
        var key = prefix == null ? k.Key : prefix + "[" + k.Key + "]";
        if (k.Value != null)
            AddKeyValuePair(nvc, key, k.Value);
    }
}

private static void AddKeyValuePair(
    NameValueCollection nvc,
    string key,
    object value)
{
    if (value is string || value.GetType().IsPrimitive)
    {
        nvc.Add(key, value.ToString());
    }
    else if (value is DateTime)
    {
        nvc.Add(key, ((DateTime)value).ToString("g"));
    }
    else
    {
        AddNonPrimitiveValue(nvc, key, value);
    }
}

private static void AddNonPrimitiveValue(
    NameValueCollection nvc,
    string key,
    object value)
{
    var a = value as JArray;
    if (a != null)
    {
        for (int i = 0; i < a.Count; i++)
            AddKeyValuePair(nvc, key + "[" + i + "]", a[i]);
    }
    else
    {
        var v = value as JValue;
        if (v != null)
        {
            AddKeyValuePair(nvc, key, v.Value);
        }
        else
        {
            var j = value as JObject;
            if (j != null)
                AddKeyValuePairs(nvc, j.ToObject<Dictionary<string, object>>(), key);
        }
    }
}
0

All Articles