标签: fastjson

如何使用FastJson从Json字符串转到Object Collection

我正在使用fastJSON,我遇到了一个问题.我无法获取JSON字符串并将其转换为对象集合.

我认为它可以解决这个问题,但也许我做错了或误解了.

处理对象的多态集合

这是我在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; …
Run Code Online (Sandbox Code Playgroud)

.net c# json fastjson

7
推荐指数
2
解决办法
1万
查看次数

修复无效JSON的最有效方法

我陷入了一种不可能的境地.我有来自外太空的JSON(他们无法改变它).这是JSON

{
    user:'180111',
    title:'I\'m sure "E pluribus unum" means \'Out of Many, One.\' \n\nhttp://en.wikipedia.org/wiki/E_pluribus_unum.\n\n\'',
    date:'2007/01/10 19:48:38',
    "id":"3322121",
    "previd":112211,
    "body":"\'You\' can \"read\" more here [url=http:\/\/en.wikipedia.org\/?search=E_pluribus_unum]E pluribus unum[\/url]'s. Cheers \\*/ :\/",
    "from":"112221",
    "username":"mikethunder",
    "creationdate":"2007\/01\/10 14:04:49"
}
Run Code Online (Sandbox Code Playgroud)

我说,"它远不是一个有效的JSON".他们的反应是"嗯!但是Javascript可以毫无怨言地阅读它":

<html>
<script type="text/javascript">
    var obj = {"PUT JSON FROM UP THERE HERE"};

    document.write(obj.title);
    document.write("<br />");
    document.write(obj.creationdate + " " + obj.date);
    document.write("<br />");
    document.write(obj.body);
    document.write("<br />");
</script>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

问题

我应该通过.NET(4)读取和解析这个字符串,它破坏了Json.org的 C#部分中提到的14个库中的3个(没有尝试其余部分).为了解决问题,我编写了以下函数来解决单引号和双引号的问题.

public static string JSONBeautify(string InStr){ …
Run Code Online (Sandbox Code Playgroud)

javascript c# json .net-4.0 fastjson

7
推荐指数
1
解决办法
8237
查看次数

fastJson反序列化未处理的异常

我正在使用fastJson库将json字符串反序列化为"Person"对象.Person类定义如下:

class Person
{
    public string type;
    public string id;
    public string name;
}
Run Code Online (Sandbox Code Playgroud)

Json字符串是:

[{
"type": "/basketball/basketball_player", 
"id": "/en/rasheed_wallace", 
"name": "Rasheed Wallace"
},
{
"type": "/basketball/basketball_player", 
"id": "/en/tayshaun_prince", 
"name": "Tayshaun Prince"
}]
Run Code Online (Sandbox Code Playgroud)

当我使用代码时:

var obj = fastJSON.JSON.Instance.ToObject<List<Person>>(str);
Run Code Online (Sandbox Code Playgroud)

它显示了一个未处理的异常

Failed to fast create instance for type
'System.Collections.Generic.List`1[[JsonSample.Person, JsonSample, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null]]' from assemebly  
System.Collections.Generic.List`1[[JsonSample.Person, JsonSample, Version=1.0.0.0, 
Culture=neutral, PublicKeyToken=null]], mscorlib, Version=4.0.0.0, Culture=neutral, 
PublicKeyToken=b77a5c561934e089'
Run Code Online (Sandbox Code Playgroud)

但是如果我使用代码,那么在Newtonsoft.Json库中一切正常:

var obj = JsonConvert.DeserializeObject<List<Person>>(str);
Run Code Online (Sandbox Code Playgroud)

那么这是fastJson的错误还是我没有以正确的方式使用fastJson?

c# json deserialization fastjson

5
推荐指数
1
解决办法
2350
查看次数

Dapper结果为json(使用fastjson)

=====更新时间2016年8月20日=====

最新版本的fastjson现在可以Dictionary<string, ?>正确处理类型,我的问题现在解决了.

=============================

我正在使用fastjson来序列化来自Dapper的查询结果,DB中的表具有如下数据:

id | name | price
1  | x    | 100
2  | y    | 200
....
Run Code Online (Sandbox Code Playgroud)

当我

using Dapper;
using fastJSON;
// ....
JSON.Parameters.KVStyleStringDictionary = false;
// ....
result = JSON.toJSON(conn.Query("SELECT * FROM tableX"));
Run Code Online (Sandbox Code Playgroud)

我希望结果如下:

[{"id":1,"name":"x","price":100},{"id":2,"name":"y","price":200},...]
Run Code Online (Sandbox Code Playgroud)

但实际结果输出:

[[{"Key":"id","Value":1},{"Key":"name","Value":"x"},{"Key":"price","Value":100}],
[{"Key":"id","Value":2},{"Key":"name","Value":"y"},{"Key":"price","Value":200}]...]
Run Code Online (Sandbox Code Playgroud)

生成了许多看起来多余的键值对.

有没有办法得到正确的结果?

或者我应该切换到另一个JSON序列化程序?

==========更新==========

makubex88的答案表明我可以创建一个映射表的自定义类,并使用它conn.Query<myClass>来获取正确的json,虽然它适用于这种情况,看起来我必须为DB中的每个表创建数百个类才能获得理想的json结果,对我来说确实很累人.(谢谢你:P)

任何替代解决方案将受到高度赞赏!

.net c# json dapper fastjson

5
推荐指数
1
解决办法
5871
查看次数

FastJSON - 如何使用?

我已经开始使用FastJSON,我遇到了一些使用它的问题.我在互联网上找不到任何指南或文档,只有CodeProject中的一些摘录.

例如:我有这个课程:

[Serializable]
public class Prueba
{
    public Prueba()
    {
        prueba1 = 5;
        prueba2 = 6;
        prueba3 = "Hola";
    }

    public int prueba1 { get; set; }
    public int prueba2 { get; set; }
    public string prueba3 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

如果我执行fastJSON.JSON.ToJSON(new Prueba())我得到这个字符串:

{"$ types":{"WebApplication3.Prueba,WebApplication3,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null":"1"},"$ type":"1","prueba1":5," prueba2 ":6," prueba3 ":" HOLA"}

但我期待这个字符串:

"{" prueba1 ":5",prueba2 ":6," prueba3 ":" HOLA "}"

如您所见,它包含了一些我不希望在字符串中的汇编信息.我曾尝试使用JSONParameters类,但我没有看到任何属性.

那么......你知道如何配置吗?你知道互联网上的任何指南或文档,以了解fastJSON的工作原理吗?

非常感谢,问候

.net c# json fastjson

2
推荐指数
1
解决办法
1415
查看次数

标签 统计

c# ×5

fastjson ×5

json ×5

.net ×3

.net-4.0 ×1

dapper ×1

deserialization ×1

javascript ×1