fastJSON ToJSON into dictionary?

How to use fastJSON (or some other JSON lib, possibly) to dump some data into a dictionary format, e.g. {"key1": "valstring", "key2": 1234}?

If I try to dump Dictionary<string, Object> I get something like [{"k":"key1","v":"valstring"},{"k":"key2","v":1234}] instead.

Answers


We use Json.NET at our office. We send json objects between python and C#. We ran into the same problem, though ours was just the differences in how the languages naturally serialized it.

The best part about it, if I'm remember correctly, is that it had this behavior right out of the box.

var dict = new Dictionary<string, string>();
dict.Add("key", "val");
dict.Add("key2", "val2");

string json = JsonConvert.SerializeObject(dict);

Json should equal { "key": "val", "key2": "val2" }


You just can use JavaScriptSerializer to create your solution, it's native for .Net.

var dict = new Dictionary<string, string>();
dict.Add("key", "val");
dict.Add("key2", "val2");

var serializer = new JavaScriptSerializer();
string json = serializer.Serialize(dict);

And you'l get result you are expected: {"key1": "valstring", "key2": 1234}


Need Your Help

Creating subclass entities in doctrine with symfony2

php symfony2 doctrine

I'm trying to create a user system in my symfony2 project where I have two types of users; buyers and sellers. Both are users, so it would make sense for them to extend a basic user class.

Pure virtual method called - cross compiling

c++ cross-compiling embedded-linux virtual-functions g++-4.7

I'm writing an event-based programming library for use on the BeagleBone Black and have encountered a strange error.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.