0

I have this JSON string coming in from a form, in there I have key-value pairs (the regular json), but I don't know what keys will come in. I want to get all the key-value pairs there, loop them, then create a row in my database for each of them. That row basically just has "[ID, fkId, Label, Value]", both Label and Value are strings.

I'm having a hard time figuring out how to parse that json string to get the info I want. Currently I don't care about nested json strings, I might handle those later / differently.

Burkhard
  • 14,596
  • 22
  • 87
  • 108
Sébastien Richer
  • 1,642
  • 2
  • 17
  • 38
  • Have you looked into [this](http://wiki.unity3d.com/index.php/SimpleJSON)? Even though it's made for Unity, I think you could still use it for other purposes. – Dion V. Sep 15 '14 at 19:01

1 Answers1

2

Just deserialize to a Dictionary<string, string>, like this:

var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();

var fieldsValues = serializer.Deserialize<Dictionary<string, string>>(@"{""A"":""B"", ""AX"":""BX""}");
// fieldsValues now contains two keys "A" and "AX"

Then you can just loop through the fieldsValues dictionary and insert into your database at your heart's content.

Menno van den Heuvel
  • 1,851
  • 13
  • 24
  • Excellent, maybe add the foreach loop to the answer to help others, I have use this : foreach (var fieldsValue in fieldsValues) {
    @fieldsValue.Key : @fieldsValue.Value
    }
    – Sébastien Richer Sep 15 '14 at 20:14