标签: json-deserialization

杰克逊 - 反序列化嵌套的JSON

我有一个JSON字符串,其格式如下:

{
  "response": { 
    "execution_status": "ready", 
    "report": {
      "cache_hit": true, 
      "created_on": "2013-07-29 08:42:42", 
      "fact_cache_error": null, 
      "fact_cache_hit": true, 
      "header_info": null, 
      "name": null, 
      "report_size": "5871", 
      "row_count": "33", 
      "url": "report-download?id=278641c223bc4e4d63df9e83d8fcb4e6"
     }, 
  "status": "OK"
  }
}
Run Code Online (Sandbox Code Playgroud)

JSON的响应部分对于一堆响应类型是常见的.此JSON的报告部分仅适用于此响应.所以我创建了一个Response类,如下所示,带有getter和setter(为简洁起见,这里没有包含getter和setter):

@JsonRootName(value = "response")
public class Response implements Serializable {

    private static final long serialVersionUID = -2597493920293381637L;

    @JsonProperty(value = "error")
    private String error;
    @JsonProperty(value = "error_code")
    private String errorCode;
    @JsonProperty(value = "error_id")
    private String errorId;
    @JsonProperty(value = "error_description")
    private String errorDescription;
    @JsonProperty(value = "method")
    private String method;
    @JsonProperty(value …
Run Code Online (Sandbox Code Playgroud)

java jackson json-deserialization

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

如何反序列化不同类型的集合?

我有一个看起来像这样的JSON提要(我删除了一些这个例子不需要的字段):

{
  "total_count": 2,
  "num_pages": 1,
  "current_page": 1,
  "balance": {
    "amount": "0.00001199",
    "currency": "BTC"
  },
  "transactions": [
    {
      "transaction": {
        "id": "5018f833f8182b129c00002f",
        "created_at": "2012-08-01T02:34:43-07:00",
        "sender": {
          "id": "5011f33df8182b142400000e",
          "name": "User Two",
          "email": "user2@example.com"
        },
        "recipient": {
          "id": "5011f33df8182b142400000a",
          "name": "User One",
          "email": "user1@example.com"
        }
      }
    },
    {
      "transaction": {
        "id": "5018f833f8182b129c00002e",
        "created_at": "2012-08-01T02:36:43-07:00",
        "hsh": "9d6a7d1112c3db9de5315b421a5153d71413f5f752aff75bf504b77df4e646a3",
        "sender": {
          "id": "5011f33df8182b142400000e",
          "name": "User Two",
          "email": "user2@example.com"
        },
        "recipient_address": "37muSN5ZrukVTvyVh3mT5Zc5ew9L9CBare"
      }
    }
 ]
}
Run Code Online (Sandbox Code Playgroud)

有两种类型的交易在这个饲料:有一个内部事务recipient有一个和外部交易hshrecipient_address.

我创建了以下类来容纳这个结构:

UML

因此,我们为所有分页结果( …

.net c# json json.net json-deserialization

8
推荐指数
1
解决办法
571
查看次数

Json.Net在列表反序列化期间调用属性getter,导致重复项

我正在使用json.net来实现winform应用程序的memento模式.我正在使用memento在失败的数据库事务上回滚对象.我得到的问题是,当反序列化纪念品时,调用getter而不是setter.让我来证明:

class MyClass
{
    public int ID { get; set; }
    public string field1 { get; set; }
    public string field2 { get; set; }

    private List<SomeObject> _someObjects;
    public List<SomeObject> SomeObjects
    {
        get
        {
            if(_someObjects == null)
            {
                _someObjects = LoadSomeObjectsFromDB();
            }
            return _someObjects;
        }
        set
        {
            _someObjects = value;
        }
    }

    private List<AnotherObject> _anotherObjects;
    public List<AnotherObject> AnotherObjects
    {
        get
        {
            if(_anotherObjects == null)
            {
                _anotherObjects= LoadAnotherObjectsFromDB();
            }
            return _anotherObjects ;
        }
        set
        {
            _anotherObjects = value;
        } …
Run Code Online (Sandbox Code Playgroud)

c# json.net winforms json-deserialization

8
推荐指数
1
解决办法
3039
查看次数

如何触发对Jackson中实现JsonSerializable的类的.serializeWithType()的调用?

这是杰克逊2.2.x.

我有一个班级实施JsonSerializable; 有两种方法可以实现这个接口,serialize()serializeWithType().

我想测试这个类的{de,}序列化,我可以serialize()轻松触发调用; 但是,不是serializeWithType().

后一种方法javadoc表示调用此方法

[...]预计其他类型信息将包含在序列化中,以便进行反序列化.

我只是不明白这意味着什么......

如何设置测试环境以便调用此方法?请注意,要序列化的JSON可以是除object之外的任何类型(即boolean,number,string,array都是有效类型).

java serialization jackson json-deserialization

8
推荐指数
1
解决办法
2946
查看次数

反序列化json序列化的CookieCollection

在我的代码中,我必须将Json序列化一个CookieCollection对象并将其作为字符串传递,为实现这一点我喜欢这样:

var json = Newtonsoft.Json.JsonConvert.SerializeObject(resp.Cookies);
Run Code Online (Sandbox Code Playgroud)

导致以下json

[
 {
  "Comment": "",
  "CommentUri": null,
  "HttpOnly": false,
  "Discard": false,
  "Domain": "www.site.com",
  "Expired": true,
  "Expires": "1970-01-01T03:30:01+03:30",
  "Name": "version",
  "Path": "/",
  "Port": "",
  "Secure": false,
  "TimeStamp": "2015-06-01T12:19:46.3293119+04:30",
  "Value": "deleted",
  "Version": 0
 },
 {
  "Comment": "",
  "CommentUri": null,
  "HttpOnly": false,
  "Discard": false,
  "Domain": ".site.com",
  "Expired": false,
  "Expires": "2015-07-31T12:19:48+04:30",
  "Name": "ADS_7",
  "Path": "/",
  "Port": "",
  "Secure": false,
  "TimeStamp": "2015-06-01T12:19:46.3449217+04:30",
  "Value": "0",
  "Version": 0
 }
]
Run Code Online (Sandbox Code Playgroud)

为了反序列化这个json我想做这样的事情:

var cookies = Newtonsoft.Json.JsonConvert.DeserializeObject<CookieCollection>(json);
Run Code Online (Sandbox Code Playgroud)

但它失败并提出JsonSerializationException

无法创建和填充列表类型System.Net.CookieCollection.路径'',第1行,第1位.

所以我将我的代码更改为以下内容并立即开始工作

var tmpcookies …
Run Code Online (Sandbox Code Playgroud)

c# serialization json.net httpcookiecollection json-deserialization

8
推荐指数
1
解决办法
2045
查看次数

aspnet:MaxJsonDeserializerMembers vs maxRequestLength

我遇到了像JSON请求太大而无法反序列化的错误..

在stackoverflow上快速搜索告诉您应该将appSetting aspnet:MaxJsonDeserializerMembers设置为更高以解决问题.但是,appSettings上的msdn文档说

  Caution
  Setting this attribute to too large a number can pose a security risk.
Run Code Online (Sandbox Code Playgroud)

我希望您不要将此值设置为更高的数字,以防止任何人提交可能影响您的系统的大型有效负载.但是,鉴于我已经将maxRequestLength的值设置为大数,将更改aspnet:MaxJsonDeserializerMembers值是否还有其他任何安全隐患?

我没有看到1001个小型json成员如何构成单个大型json对象的安全威胁.

c# asp.net asp.net-mvc json json-deserialization

8
推荐指数
1
解决办法
9897
查看次数

如何让杰克逊反序列化到我自己的Array实现中

鉴于我自己的数组实现MyArray<T>,如何让Jackson知道它,以便它能够从JSON数组反序列化为MyArray<T>?到目前为止,我只得到这个例外:

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of MyArray out of START_ARRAY token
Run Code Online (Sandbox Code Playgroud)

java json jackson json-deserialization

8
推荐指数
1
解决办法
761
查看次数

反序列化DataTable后,DateTime列类型变为String类型

我有一个DataTable有两列.ShipmentDate(DateTime)和Count(Int).在我反序列化字符串之后,我注意到如果第一个itemarray值为null,ShipmentDate的类型就变成了字符串.

请查看以下示例.除第一个数组项外,json字符串都有相同的数据.

string jsonTable1 = "[{\"ShipmentDate\":null,\"Count\":3},{\"ShipmentDate\":\"2015-05-13T00:00:00\",\"Count\":13},{\"ShipmentDate\":\"2015-05-19T00:00:00\",\"Count\":1},{\"ShipmentDate\":\"2015-05-26T00:00:00\",\"Count\":1},{\"ShipmentDate\":\"2015-05-28T00:00:00\",\"Count\":2}]";
string jsonTable2 = "[{\"ShipmentDate\":\"2015-05-13T00:00:00\",\"Count\":13},{\"ShipmentDate\":null,\"Count\":3},{\"ShipmentDate\":\"2015-05-19T00:00:00\",\"Count\":1},{\"ShipmentDate\":\"2015-05-26T00:00:00\",\"Count\":1},{\"ShipmentDate\":\"2015-05-28T00:00:00\",\"Count\":2}]";

DataTable tbl1 = Newtonsoft.Json.JsonConvert.DeserializeObject<DataTable>(jsonTable1);
DataTable tbl2 = Newtonsoft.Json.JsonConvert.DeserializeObject<DataTable>(jsonTable2);

Console.WriteLine(tbl1.Columns["ShipmentDate"].DataType);
Console.WriteLine(tbl2.Columns["ShipmentDate"].DataType);
Run Code Online (Sandbox Code Playgroud)

在我的场景中,第一个项目数组的ShipmentDate可以为null,并通过将其转换为字符串类型来创建问题.

我有一种情况,数据表的模式是动态的.我无法创建强类型类.

c# datetime json json.net json-deserialization

8
推荐指数
1
解决办法
2493
查看次数

如何在abap中反序列化匿名JSON数组?

我有一个我要反序列化的匿名数组.这里的例子,所有对象都是一样的,我只编写了第一个.

[
  { "time":"08:55:54",
    "date":"2016-05-27",
    "timestamp":1464332154807,
    "level":3,
    "message":"registerResourcePath ('', '/sap/bc/ui5_ui5/ui2/ushell/resources/')",
    "details":"","component":"sap.ui.ModuleSystem"},
  {"time":"08:55:54","date":"2016-05-27","timestamp":1464332154808,"level":3,"message":"URL prefixes set to:","details":"","component":"sap.ui.ModuleSystem"},
  {"time":"08:55:54","date":"2016-05-27","timestamp":1464332154808,"level":3,"message":"  (default) : /sap/bc/ui5_ui5/ui2/ushell/resources/","details":"","component":"sap.ui.ModuleSystem"}
]
Run Code Online (Sandbox Code Playgroud)

我尝试使用CL_TREX_JSON_SERIALIZER进行反序列化,但是这个东西已经损坏,并且不适用于我的JSON (看看这个问题)

然后我尝试了/ UI2/CL_JSON.这个东西需要一个完全适合JSON对象给出的Object的"结构".在我的例子中,"结构"是指具有属性time, date, timestamp, level, message和的对象的内部表details.有问题:这个类没有正确处理reffrences:它使用classdescription来描述分配给fieldsymbol的字段.因为我不能有一个对象列表,但只有一个对象的依赖列表,通常没有问题.该解决方案既不起作用

作为第三次尝试,我尝试使用Horst KellerCALL TRANSFORMATION所描述的,但是使用这种方法,我无法读取匿名数组.(Jagger评论了这个ABAP 2 JSON和JSON 2 ABAP与ST的更好的联系)

我的主要观点:

  • 我不想改变JSON,因为这是我得到的 sap.ui.log
  • 我更喜欢使用内置功能而不是第三方框架

abap json-deserialization

8
推荐指数
1
解决办法
4988
查看次数

如何用字符串表示反序列化枚举?

我想创建一个类,它有一个枚举作为属性。此枚举应具有字符串表示形式,在将使用枚举属性的类实例转储为 JSON 字符串时,该表示形式显示为人类可读的值。在下面的最小工作示例中,我以三种不同的方式创建了三个枚举。

反序列化后,每个属性都向我们表明它来自枚举,但带有字符串表示形式的枚举除外。它只是一个字符串。

如果不可能实现这样的结构,我想知道为什么。

要求

如果你想测试它,你必须安装jsonsattrs使用

pip install attrs jsons
Run Code Online (Sandbox Code Playgroud)

最小工作示例

在这里您可以看到一个最小的工作示例。

import jsons
import attr

from enum import Enum

# ----------------------------------------------------
# create enumeration with the help of a dictionary

fruits = {
    "PINEAPPLE": "PINEAPPLE",
    "APPLE": "APPLE",
    "ORANGE": "ORANGE",
    "BANANA": "BANANA",
}

Fruit = Enum("FRUITS", fruits)

# ----------------------------------------------------
# create a classical enumeration


class Nut(Enum):

    PEANUT = 1
    HAZELNUT = 2
    CASHEW = 3
    WALNUT = 4


# ----------------------------------------------------
# create enumeration with a …
Run Code Online (Sandbox Code Playgroud)

python enums serialization json-deserialization python-attrs

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