How to extract information from an object created by JavaScriptSerializer

I am developing a C # application that can manage SqueezeboxServer (SBS). Communication with SBS is done using JSON messages for http: // serverIP: 9000 / jsonrpc.js Therefore, I send JSON messages through HTTPWepRequest and receive responses through HTTPWebResponse.

The answer I get is a JSON string. And here the problems begin ... In the meantime, I will convert the JSON message to an object using JavaScriptSerializer. It looks like this:

public static Object FromJSON(this string reply)
{
    JavaScriptSerializer deSerializer = new JavaScriptSerializer();
    return deSerializer.DeserializeObject(reply);
}

This code gives me an object that contains the data I request. The data I request can be very different. Sometimes the answer is one answer, but in other situations it can be several things.

Let's look at the two images that I included:

The first shows the object after it was returned by deSerializer. You can see that the object is a dictionary with 4 key-value pairs. The KVP I'm interested in is fourth. The key β€œresult” is the one that contains the data I need. But this key has another Dictonary as its value. And this will continue until I get real data, namely the name of the album and its identifier.

alt text

In the second image, I want to get the value 0, which belongs to the key "_count". As you can see, this object is less complicated.

alt text

So, the essence of my question is how can I find a solution that can get the information I need, but works with different types of objects (like at different depths)?

Hope someone can send me in the right direction.

Thank!

+3
1

JavaScriptConverter, .

        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 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;
            }

        }
    }

var serializer = new JavaScriptSerializer(); 
serialzer.RegisterConverters( new[]{ new DataObjectJavaScriptConverter() } ); 
var dataObj = serializer.Deserialize<DataObject>( json ); 

JavaScriptSerializer.Deserialize -

0

Source: https://habr.com/ru/post/1751036/


All Articles