简体   繁体   中英

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 and 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM