How to extract information from an Object produced by JavaScriptSerializer
I'm developing an application in C# which can control a SqueezeboxServer(SBS). Communicating to the SBS is via JSON messages to http://serverIP:9000/jsonrpc.js So I send JSON messages via a HTTPWepRequest and get answers via an HTTPWebResponse.
The answer I get is a String in JSON notation. And this is where the problems start... For now I convert the JSON message to a Object with the JavaScriptSerializer. This goes like this:
public static Object FromJSON(this string reply)
{
JavaScriptSerializer deSerializer = new JavaScriptSerializer();
return deSerializer.DeserializeObject(reply);
}
This code gives me an object which holds the data I ask for. The data I ask for can be very different. Sometimes the answer is a single answer while in other situations it can be multiple things.
Let's consider the two images I've included:
The first one shows the object after it has been returned by the deSerializer. You can see the object is a Dictionary with 4 key-value pairs. The kvp I'm interrested in, is the 4th one. The key "result" is the one which hold the data I need. But this key has another Dictonary as a value. And this goes on a开发者_如何学Gond on until the actual data I want, which is the album name and its ID.
In the second image the data I want is the value 0 which belongs to the "_count" key. As you can see, this object is less complicated.
So the bottomline of my question is how do I make a solution which can retrieve the information I want but works with differt kind of objects (as in different depths)?
Hope anybody can send me in the right direction.
Thanks!
You can use a JavaScriptConverter to get better control of the deserialization experience.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Web.UI.WebControls;
using System.Collections;
namespace System.Web.Script.Serialization.CS {
public class ListItemCollectionConverter: JavaScriptConverter {
public override IEnumerable <Type> SupportedTypes {
//Define the ListItemCollection as a supported type.
get {
return new ReadOnlyCollection <Type> (new List <Type> (new Type[] {
typeof(ListItemCollection)
}));
}
}
public override IDictionary <string, object> Serialize(object obj, JavaScriptSerializer serializer) {
ListItemCollection listType = obj as ListItemCollection;
if (listType != null) {
// Create the representation.
Dictionary <string, object> result = new Dictionary <string, object> ();
ArrayList itemsList = new ArrayList();
foreach(ListItem item in listType) {
//Add each entry to the dictionary.
Dictionary <string, object> listDict = new Dictionary <string, object> ();
listDict.Add("Value", item.Value);
listDict.Add("Text", item.Text);
itemsList.Add(listDict);
}
result["List"] = itemsList;
return result;
}
return new Dictionary <string, object> ();
}
public override object Deserialize(IDictionary <string, object> dictionary, Type type, JavaScriptSerializer serializer) {
if (dictionary == null)
throw new ArgumentNullException("dictionary");
if (type == typeof(ListItemCollection)) {
// Create the instance to deserialize into.
ListItemCollection list = new ListItemCollection();
// Deserialize the ListItemCollection's items.
ArrayList itemsList = (ArrayList) dictionary["List"];
for (int i = 0; i < itemsList.Count; i++)
list.Add(serializer.ConvertToType <ListItem> (itemsList[i]));
return list;
}
return null;
}
}
}
Then deserialize it
var serializer = new JavaScriptSerializer();
serialzer.RegisterConverters( new[]{ new DataObjectJavaScriptConverter() } );
var dataObj = serializer.Deserialize<DataObject>( json );
JavaScriptSerializer.Deserialize - how to change field names
精彩评论