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.

1
  • Have you looked into this? Even though it's made for Unity, I think you could still use it for other purposes. Commented Sep 15, 2014 at 19:01

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent, maybe add the foreach loop to the answer to help others, I have use this : foreach (var fieldsValue in fieldsValues) { <div>@fieldsValue.Key : @fieldsValue.Value</div> }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.