如何使用FastJson从Json字符串转为对象集合
我正在使用 fastJSON ,但遇到了问题。我无法获取 JSON 字符串并将其转换为对象集合。
我认为它可以处理这个问题,但也许我做错了或被误解了。
处理对象的多态集合
这是我在 C# 命令行应用程序中所做的一个示例(只需下载 .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
始终为 null。
我使用 fastJSON 是因为我需要一些真正不依赖于 .NET 库的东西,并且我使用 monodroid(可能后来是 monotouch),并且它对可以使用和不能使用的内容非常挑剔。
例如,我无法使用 Json.net 库(我认为有一个用于 monodroid 的库,但我试图在处理 iPhone 部分时使我的代码可重用)。
I am using fastJSON and I ran into a problem. I cannot take a JSON string and convert it to a collection of objects.
I thought it could handle this but maybe I am doing it wrong or misunderstood.
Handles polymorphic collections of objects
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).
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.
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.
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请注意,从 fastJSON 2.x 开始,OP 代码基本上应该可以正常工作(请注意,语法略有变化)。
Note that as of fastJSON 2.x, the OP code should basically work fine (note that the syntax has changed slightly).
您不应该使用
ToObject
来反序列化数组。相反,您应该使用Parse
方法来解析 JSON。使用
ToObject
时,假设您有一个正在反序列化的 JSON 对象实例(不是数组或标量值),使用Parse
时,它将处理序列化为 JSON 的任何类型并返回适当的类型。在这种情况下,调用
Parse
并向其传递jsonResult
将返回ArrayList
其中包含三个实例:问题是现在您有一个
ArrayList
包含一些具有映射到属性名称的标量值的Dictionary
实例(或其他Dictionary
实例,如果是引用)。我将其归类为错误。我希望数组的解析能够正确处理这个问题。
您可以修改 ParseArray 的代码,以便在调用 array.Add 时嗅探类型。
这仍然留下了
ParseNumber
(可能会被调用)返回字符串的问题。这可能会也可能不会影响您。我会进行您需要的任何更改,并在 CodePlex 项目网站上使用问题跟踪器提交问题。
You shouldn't use
ToObject
to deserialize an array. Instead, you should use theParse
method to parse the 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), withParse
, it will handle any type that is serialized into JSON and return the appropriate type.In this case, calling
Parse
and passingjsonResult
to it will return anArrayList
which contains the three instances:The problem with that is that now you have an
ArrayList
containing a number ofDictionary<string, object>
instances which have the the scalar values (or otherDictionary<string, object>
instances, in the case of references) mapped to the property name.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 toarray.Add
is made.That still leaves an issue with
ParseNumber
(which could potentially be called) returning a string. 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.