9

I have a configuration file in the following JSON format:

{

  "key1": "value1",  
  "key2": "value2",  
  "key3": false,  
  "key4": 10,  

}

The user can set/unset the configuration values using a text editor. I however need to read it in my C# application. Whats the best way to do so for JSON? The above keys are not associated with a class.

3 Answers 3

5

Take a look at Json.NET: http://json.codeplex.com

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

1 Comment

I took a look at it earlier. It recommends using LINQ - but was not sure how to go about using it. My configuration file is in the form of a text file! Thanks.
3

Would this work for you?

        System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
        string json = @"{
                          'key1': 'value1',  
                          'key2': 'value2',  
                          'key3': false,  
                          'key4': 10
                        }";
        Dictionary<string, string> dic = js.Deserialize<Dictionary<string, string>>(json); // deserialize

        foreach (KeyValuePair<string,string> o in dic)
        {
            // do whatever
        }


        dic.Add("newKey", "new value"); // add an attribute

        string newjson = js.Serialize(dic);  // serialize back to string

1 Comment

I have a json array like [{'key1': 'value1' , 'key2': 'value2'},{'key1': 'value1' , 'key2': 'value2'}, {'key1': 'value1' , 'key2': 'value2'}] How to convert that.Can you pls help.
0

Alternatively, you can have a look at the JSONReaderWriterFactory from the .Net 3.5SP1 stack.

Comments

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.