如何在 Windows Phone 7 上将 JSON 解析为动态对象? [英] How to parse JSON to a dynamic object on Windows Phone 7?

查看:14
本文介绍了如何在 Windows Phone 7 上将 JSON 解析为动态对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于网络应用程序,我可以使用 System.Web 并使用此技巧将 JSON 转换为动态对象.

For web applications I can use System.Web and use this trick to convert JSON to a dynamic object.

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

But for Windows Phone I can't use JavaScriptConverter. What is the workaround to convert JSON in a dynamic object on Windows Phone 7.1?

推荐答案

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

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

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

如果 WP7 支持动态对象:

If WP7 supports 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();
    }
}

这篇关于如何在 Windows Phone 7 上将 JSON 解析为动态对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆