c# - 如何在 Windows Phone 7 上将 JSON 解析为动态对象?

标签 c# json windows-phone-7 windows-phone-7.1

对于 Web 应用程序,我可以使用 System.Web and use this trick将 JSON 转换为动态对象。

但是对于 Windows Phone,我不能使用 JavaScriptConverter。在 Windows Phone 7.1 上转换动态对象中的 JSON 的解决方法是什么?

最佳答案

Json.Net ( http://james.newtonking.com/pages/json-net.aspx )

-----编辑-----

如果 WP7 支持 DynamicObject:

using System;
using System.Dynamic;
using System.Collections;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public class JSonTest 
{
    public static void Main()
    {
        string jsonStr = @"
            { 
                'glossary': { 
                    'title': 'example glossary', 
                    'GlossDiv': { 
                        'title': 'S', 
                        'GlossList': { 
                            'GlossEntry': { 
                                'ID': 'SGML', 
                                'SortAs': 'SGML', 
                                'GlossTerm': 'Standard Generalized Markup Language', 
                                'Acronym': 'SGML', 
                                'Abbrev': 'ISO 8879:1986', 
                                'GlossDef': { 
                                    'para': 'A meta-markup language, used to create markup languages such as DocBook.', 
                                    'GlossSeeAlso': ['GML','XML'] 
                                }, 
                                'GlossSee': 'markup' 
                            } 
                        } 
                    } 
                } 
            }
        ";

        JObject o = (JObject)JsonConvert.DeserializeObject(jsonStr);
        dynamic json = new JsonObject(o);
        Console.WriteLine(json.glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso.Length);
        Console.WriteLine(json.glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso[1]);
        foreach (var x in json.glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso)
        {
            Console.WriteLine(x);
        }
        Console.ReadLine();
    }
}

class JsonObject : DynamicObject,IEnumerable,IEnumerator
{
    object _object;

    public JsonObject(object jObject)
    {
        this._object = jObject;
    }

    public object this[int i]
    {
        get
        {
            if (!(_object is JArray)) return null;

            object obj = (_object as JArray)[i];
            if (obj is JValue)
            {
                return ((JValue)obj).ToString();
            }
            return new JsonObject(obj);
        }
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        result = null;

        if (_object is JArray && binder.Name == "Length")
        {
            result = (_object as JArray).Count;
            return true;
        }

        JObject jObject = _object as JObject;
        object obj = jObject.SelectToken(binder.Name);

        if (obj is JValue)
            result = ((JValue)obj).ToString();
        else
            result = new JsonObject(jObject.SelectToken(binder.Name));

        return true;
    }

    public override string ToString()
    {
        return _object.ToString();
    }

    int _index = -1;

    public IEnumerator GetEnumerator()
    {
        _index = -1;
        return this;
    }

    public object Current
    {
        get 
        {
            if (!(_object is JArray)) return null;
            object obj = (_object as JArray)[_index];
            if (obj is JValue) return ((JValue)obj).ToString();
            return obj;
        }
    }

    public bool MoveNext()
    {
        if (!(_object is JArray)) return false;
        _index++;
        return _index <(_object as JArray).Count;
    }

    public void Reset()
    {
        throw new NotImplementedException();
    }
}

关于c# - 如何在 Windows Phone 7 上将 JSON 解析为动态对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7626717/

相关文章:

c# - 等价于 ContinueWith(delegate, CancellationToken) with await continuation

windows-phone-7 - 程序集上的 GetExportedTypes() 抛出 NotSupportedException

c# - Windows Phone 7 事件调用?

c# - 我们如何在将数据绑定(bind)到 Windows Phone 中的文本 block 时处理空值

c# - 如何检测嵌套属性的变化?

c# - 内部类构造函数...这有效吗?

c# - 在 Windows 应用商店应用程序中获取 CoreDispatcher 的正确方法

mysql - 如何在 MySQL 中编写可以解析列中 JSON 数据的查询?

java - 如何通过在android中前后添加双引号来传递文本

javascript - 使用 AJAX/JSON 提供静态 HTML 和生成内容有什么好处?