简体   繁体   English

如何使用 FastJson 到 go 从 Json 字符串到 Object 集合

[英]How to use FastJson to go from Json string to Object Collection

I am using fastJSON and I ran into a problem.我正在使用fastJSON ,但遇到了问题。 I cannot take a JSON string and convert it to a collection of objects.我无法获取 JSON 字符串并将其转换为对象集合。

I thought it could handle this but maybe I am doing it wrong or misunderstood.我认为它可以处理这个问题,但也许我做错了或被误解了。

Handles polymorphic collections of objects处理对象的多态 collections

Here is an example I did in a C# cmd line app (just download the.cs files and add to a project and copy the follow code to test).这是我在 C# cmd 行应用程序中所做的示例(只需下载 .cs 文件并添加到项目中并复制以下代码进行测试)。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Class1> store = new List<Class1>();
            for (int i = 0; i < 3; i++)
            {
                Class1 c = new Class1();
                c.Name = "test";
                c.Start = DateTime.Now;
                store.Add(c);
            }

           string jsonResult = fastJSON.JSON.Instance.ToJSON(store);

           List<Class1> backToObject = fastJSON.JSON.Instance.
               ToObject<List<Class1>>(jsonResult);
        }
    }

    public class Class1
    {
        public string Name { get; set; }
        public DateTime Start { get; set; }
    }
}

backToObject is always null. backToObject始终为 null。

I am using fastJSON because I need something that really has no dependencies on .NET libraries and I am using monodroid (and probably later monotouch) and it is very picky in what you can use and can't use.我正在使用 fastJSON,因为我需要一些真正不依赖于 .NET 库的东西,而且我使用的是 monodroid(可能是后来的 monotouch),它对你可以使用和不能使用的东西非常挑剔。

For instance I can not use the Json.net library (I think there is one for monodroid but I trying to make my code reusable for when I do the iPhone part).例如,我不能使用 Json.net 库(我认为有一个用于 monodroid 但我试图让我的代码在我做 iPhone 部分时可重用)。

Note that as of fastJSON 2.x, the OP code should basically work fine (note that the syntax has changed slightly).请注意,从 fastJSON 2.x 开始,OP 代码应该基本上可以正常工作(请注意,语法略有变化)。

List<Class1> wallets = JSON.ToObject<List<Class1>>(json);

You shouldn't use ToObject to deserialize an array.您不应该使用ToObject来反序列化数组。 Instead, you should use the Parse method to parse the JSON.相反,您应该使用Parse方法来解析 JSON。

When using ToObject , you are assuming that you have an instance of an object in JSON that is being deserialized (not an array, or a scalar value), with Parse , it will handle any type that is serialized into JSON and return the appropriate type. When using ToObject , you are assuming that you have an instance of an object in JSON that is being deserialized (not an array, or a scalar value), with Parse , it will handle any type that is serialized into JSON and return the appropriate type .

In this case, calling Parse and passing jsonResult to it will return an ArrayList which contains the three instances:在这种情况下,调用Parse并将jsonResult传递给它将返回一个ArrayList ,其中包含三个实例:

ArrayList arrayList = fastJSON.JSON.Instance.parse(jsonResult) as ArrayList;

The problem with that is that now you have an ArrayList containing a number of Dictionary<string, object> instances which have the the scalar values (or other Dictionary<string, object> instances, in the case of references) mapped to the property name.问题在于,现在您有一个ArrayList包含许多Dictionary<string, object>实例,这些实例具有映射到属性名称的标量值(或其他Dictionary<string, object>实例,在引用的情况下) .

I'd classify this as a bug.我会将其归类为错误。 I'd expect the parsing of an array to handle this correctly.我希望数组的解析能够正确处理这个问题。

You could modify the code for ParseArray to sniff the type when the call to array.Add is made.您可以修改array.Add ParseArray嗅探类型。

That still leaves an issue with ParseNumber (which could potentially be called) returning a string.这仍然会导致ParseNumber (可能会被调用)返回一个字符串。 This might or might not impact you.这可能会或可能不会影响您。

I'd make whatever changes you need as well as file an issue with the issue tracker on the CodePlex project site .我会进行您需要的任何更改,并在 CodePlex 项目站点上向问题跟踪器提交问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM