在 C# 中使用 JavascriptSerializer 解析未知 JSON

发布于 2025-01-01 11:05:44 字数 796 浏览 2 评论 0 原文

如何使用 JavaScriptSerializer 解析一些未知的动态 JSON。特别是,我正在为 Google Calendar API 编写自己的包装器。事件有一个名为 ExtendedProperties 的对象,其中包含一个私有对象和共享对象,其中包含一组未知的属性:

"extendedProperties": {
    "private": {
        "UnknownKey1": "UnknownValue1",
        "UnknownKey2": "UnknownValue2",
        "UnknownKey3": "UnknownValue3"
    },
    "shared": {
        "UnknownKey1": "UnknownValue1",
        "UnknownKey2": "UnknownValue2",
        "UnknownKey3": "UnknownValue3"
    }
}

我想为 JavaScriptSerializer 创建一个这样的类:

public class ExtendedProperties
{
    public ??? @private { get; set; }
    public ??? shared { get; set; }
}

当然存在问题。

(1) 序列化程序是否理解 & 符号,以便解析属性“private”?

(2) JavaScriptSerializer 可以读/写的属性的返回类型是什么?某种字典?

提前致谢!

How can I use JavaScriptSerializer to parse some unknown dynamic JSON. In particular, I'm writing my own wrapper for the Google Calendar API. An event has an object called extendedProperties with both a private object and shared object containing an unknown set of properties:

"extendedProperties": {
    "private": {
        "UnknownKey1": "UnknownValue1",
        "UnknownKey2": "UnknownValue2",
        "UnknownKey3": "UnknownValue3"
    },
    "shared": {
        "UnknownKey1": "UnknownValue1",
        "UnknownKey2": "UnknownValue2",
        "UnknownKey3": "UnknownValue3"
    }
}

I want to create a class like this for the JavaScriptSerializer:

public class ExtendedProperties
{
    public ??? @private { get; set; }
    public ??? shared { get; set; }
}

Of course there are problems.

(1) Does the serializer understand the ampersand so it will parse the property 'private'?

(2) What would the return type be for the properties that the JavaScriptSerializer could read/write? Some sort of Dictionary?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掩饰不了的爱 2025-01-08 11:05:44
var serializer = new JavaScriptSerializer();
var jsonObject = serializer.Deserialize<IDictionary<string, object>>(jsonStr);

我使用此代码来反序列化未知的 json 对象。

var serializer = new JavaScriptSerializer();
var jsonObject = serializer.Deserialize<IDictionary<string, object>>(jsonStr);

I have used this code to deserialize unknown json objects.

梦醒时光 2025-01-08 11:05:44

解析器理解 @ 符号。如果您使用 .net 4,则可以使用 dynamic 作为类型。您可以尝试 Dictionary 尽管我在序列化和反序列化方面一直遇到问题字典指向同一参考对象。 List> 通常可以解决问题。

The parser understands the @ symbol. You can use dynamic as your type if you're using .net 4. You could try Dictionary<string,string> although I've always had problems with serializing and deserializing dictionaries to the same reference object. List<KeyValuePair<string, string>> usually does the trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文