0

I know what JSON is, but I don't understand when you deserialize a simple json property/value pair in a string of a bunch of property/value pairs like this: {"fileName":"SomeName, "FilePath":"SomePath"} then what is the object and string representing in your dictionary after being deserialized?

All we have here is a key/value pair which to me would be one object, but what's the string in the dictionary, the key right? But what kind of key and why then would I need a dictionary to deserialize this to? Why not just List and object would just contain your key/value pair? or maybe List which would be same as object, but you're using a custom type.

I just want to understand when deserialized to a dictionary, what string and object are in the dictionary after being deserialized..more so what the string is in Dictionary.

I don't think I even need a here right? because there's no root object, just key/value (property/value) pairs so wouldn't be fine for each json property/value?

user1286569
  • 33
  • 1
  • 6
  • JSON, strictly speaking, does not define ordering for objects. `[1,2,3]` would be deserialized to a List, perhaps, but `{k:v}` is not a list. It is a Map. –  Mar 29 '12 at 04:04
  • so really that's ONE json object I have in that example json above...with a bunch of properties.... – user1286569 Mar 29 '12 at 04:06
  • That is one way to look at it, yes. –  Mar 29 '12 at 04:29

2 Answers2

2

From JSON perspective, {"fileName":"SomeName, "FilePath":"SomePath"} represents a object with given properties and respective values. You must also note that, in java-script, every object is essentially a dictionary/map but as such there is no dictionary data type in JSON representation - JSON specs uses term object to refer to a collection of name/value pairs. Quoted from specs:

JSON is built on two structures:

A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.

An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

Coming to the de-serialization, deserialization in JS is anyway an object (and also a dictionary because that's how JS objects are). Coming to deserialization to C#, said JSON can be deserialized into different types that maps to the same JSON form. For example, the said JSON could be deserialized to a Dictionary<string, string> or a instance of class with string properties of names fileName and FilePath (or it could be HashTable or it could a structure/class that have multitude of properties but only corresponding two are marked for serialization).

So typically, how you use a particular deserializer would decide the type after deserialization - AFAIK, .NET provided serializers accepts type to deserialize and produces a instance of that type. For Dictionary<string, string> deserialization, JSON.NET is the answer - see How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?

Community
  • 1
  • 1
VinayC
  • 47,395
  • 5
  • 59
  • 72
  • would rather just take advantage of the JavaScriptSerializer, not use a whole library just for something so simple as to get one object from json that simply has a bunch of properties..one level no nested trees or anything in it. I a not a fan pf using a huge 3rd party framework like JSON.NET until something gets so complex that I HAVE to. This has to be simple with the existing JavaScriptSerializer without having to resort to reference a whole library like JSON.NET for this example. – user1286569 Mar 29 '12 at 04:58
  • @user1286569, yes - I agree with u but I am also a bit vary about using `JavaScriptSerializer` as MS once tried to make it obsolete (http://stackoverflow.com/questions/536359/why-microsoft-made-javascriptserializer-obsolete-prior-to-net-3-5-sp1-and-again) The alternative data-contract based serializer needs a type to specified while conversion. Let's hope .NET 4.5 would simplify the matter with introduction of `System.Json` - http://msdn.microsoft.com/en-us/library/system.json%28v=vs.110%29.aspx – VinayC Mar 29 '12 at 06:00
0

{"fileName":"SomeName, "FilePath":"SomePath"} is one object with 3 properties. Since objects in JavaScript are essentially dictionaries of properties the most obvious way to represent them in other languages is corresponding Dictionary/HashTable.

In .Net it is Dictionary<string, object> since name of propertye is always string, but value can have any type supported by JSON serialization(string, bool, array, object or number).

So result of new JavaScriptSerializer().DeserializeObject("{\"fileName\":\"SomeName\", \"FilePath\":\"SomePath\"}") is an object of type Dictionary<string, object> that contains list of all 2 properties of your object: string key "fileName" with corresponding value "SomeName" (type string, stored as object in the dictionary) and "FilePath" with value "SomePath".

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179