如何在Mono中反序列化JSON字符串?

时间:2022-11-24 22:14:13

How can I deserialize a JSON string in C# (Mono)?

如何在c# (Mono)中反序列化JSON字符串?

Is there a JSON library and instructions on how to install it? I'm using fedora 14.

有JSON库和如何安装的说明吗?我使用fedora 14。

3 个解决方案

#1


15  

we are not utilizing json contract serializer - instead we use Json.NET. it should work with mono too.
you don't need to install the assembly, just add a reference and supply it with your final package!

我们没有使用json契约序列化器——而是使用Json.NET。它也应该与mono一起工作。您不需要安装程序集,只需添加一个引用并为其提供最终的软件包!

EDIT:
how to add a reference? though i'm not a mono-devlop-er ... taken from here:

编辑:如何添加引用?虽然我不是独断专行的人……来自:

References -> Edit References -> .NET Assembly -> Browse to file & select it

引用->编辑引用-> .NET程序集->浏览到文件并选择它

otherwise: hey, just hit F1!

否则:嘿,按F1!

#2


3  

I recommend looking at ServiceStack.NET Text. It is incredibly fast compared to JSON.NET.

我建议您查看ServiceStack。网络文本。它比JSON.NET要快得难以置信。

Examples of serializing a dictionary:

序列化字典的例子:

ServiceStack.NET

ServiceStack.NET

var jsonSerializer = new JsonSerializer<Dictionary<String, Object>>();
var result = jsonSerializer.SerializeToString(dict);

JSON.NET

JSON.NET

var result = JsonConvert.SerializeObject(dict, Formatting.Indented);

#3


3  

Take a look at JSON.NET:

看看JSON.NET:

http://components.xamarin.com/gettingstarted/json.net

http://components.xamarin.com/gettingstarted/json.net

Features:

特点:

  • Flexible JSON serializer for converting between .NET objects and JSON
  • 灵活的JSON序列化器,用于在。net对象和JSON之间进行转换
  • LINQ to JSON for manually reading and writing JSON
  • 用于手动读写JSON的LINQ到JSON
  • High performance, faster than .NET's built-in JSON serializers
  • 高性能,比。net内置的JSON序列化器要快
  • Write indented, easy to read JSON
  • 写入缩进,易读JSON。
  • Convert JSON to and from XML
  • 将JSON转换为XML和XML

Example to serialize and deserialize:

序列化和反序列化示例:

    using Newtonsoft.Json;
    ...

    public class Person
    {
        public string Name { get; set; }
        public DateTime Birthday { get; set; }
    }

    void PersonToJsonToPersonExample ()
    {
        var person = new Person { Name = "Bob", Birthday = new DateTime (1987, 2, 2) };

        var json = JsonConvert.SerializeObject (person);
        Console.WriteLine ("JSON representation of person: {0}", json);

        var person2 = JsonConvert.DeserializeObject<Person> (json);
        Console.WriteLine ("{0} - {1}", person2.Name, person2.Birthday);
    }

#1


15  

we are not utilizing json contract serializer - instead we use Json.NET. it should work with mono too.
you don't need to install the assembly, just add a reference and supply it with your final package!

我们没有使用json契约序列化器——而是使用Json.NET。它也应该与mono一起工作。您不需要安装程序集,只需添加一个引用并为其提供最终的软件包!

EDIT:
how to add a reference? though i'm not a mono-devlop-er ... taken from here:

编辑:如何添加引用?虽然我不是独断专行的人……来自:

References -> Edit References -> .NET Assembly -> Browse to file & select it

引用->编辑引用-> .NET程序集->浏览到文件并选择它

otherwise: hey, just hit F1!

否则:嘿,按F1!

#2


3  

I recommend looking at ServiceStack.NET Text. It is incredibly fast compared to JSON.NET.

我建议您查看ServiceStack。网络文本。它比JSON.NET要快得难以置信。

Examples of serializing a dictionary:

序列化字典的例子:

ServiceStack.NET

ServiceStack.NET

var jsonSerializer = new JsonSerializer<Dictionary<String, Object>>();
var result = jsonSerializer.SerializeToString(dict);

JSON.NET

JSON.NET

var result = JsonConvert.SerializeObject(dict, Formatting.Indented);

#3


3  

Take a look at JSON.NET:

看看JSON.NET:

http://components.xamarin.com/gettingstarted/json.net

http://components.xamarin.com/gettingstarted/json.net

Features:

特点:

  • Flexible JSON serializer for converting between .NET objects and JSON
  • 灵活的JSON序列化器,用于在。net对象和JSON之间进行转换
  • LINQ to JSON for manually reading and writing JSON
  • 用于手动读写JSON的LINQ到JSON
  • High performance, faster than .NET's built-in JSON serializers
  • 高性能,比。net内置的JSON序列化器要快
  • Write indented, easy to read JSON
  • 写入缩进,易读JSON。
  • Convert JSON to and from XML
  • 将JSON转换为XML和XML

Example to serialize and deserialize:

序列化和反序列化示例:

    using Newtonsoft.Json;
    ...

    public class Person
    {
        public string Name { get; set; }
        public DateTime Birthday { get; set; }
    }

    void PersonToJsonToPersonExample ()
    {
        var person = new Person { Name = "Bob", Birthday = new DateTime (1987, 2, 2) };

        var json = JsonConvert.SerializeObject (person);
        Console.WriteLine ("JSON representation of person: {0}", json);

        var person2 = JsonConvert.DeserializeObject<Person> (json);
        Console.WriteLine ("{0} - {1}", person2.Name, person2.Birthday);
    }