NewtonSoft JSON不解析数组中的引用项

时间:2018-04-11 18:01:18

标签: json json.net

我编写的Web API除了带有引用的JSON之外,我使用JsonPropertyAttributeJsonObjectAttribute来标记可以被其他属性引用的属性/类。 当我从API接收数据时,它按预期工作,在JSON中设置了相应的$id$ref属性。但是,当我向API发布相同的JSON日期时,它不会解析所有数据。 Bellow是我从API收到的数据,我也回复了。

{
  "GeneralDetails": {
    "Description": "23",
    "ScheduleInfo": {
        "ScheduleFrequency": 701,
        "ScheduledDay": null,
        "ScheduledMonth": null,
        "ScheduledTime": "2018-04-10T21:30:01"
    },
    "ImagePath": "",
    "BrandId": 23,
    "Name": "23",
    "Id": 29
},
"TargetDetails": {
    "Period": 0,
    "Type": 2701,
    "Targets": [
        {
            "TierId": 34,
            "Value": 29
        }
    ]
},
"PointsDetails": [
    {
        "BooleanExperession": {
            "$id": "1",
            "$type": "Payback.TradeTool.Entities.Model.Offer.OfferRuleBooleanExpression, Payback.TradeTool.Entities",
            "Variable1": {
                "$id": "2",
                "$type": "Payback.TradeTool.Entities.Model.Offer.OfferRulePreDefinedVariable, Payback.TradeTool.Entities",
                "GroupId": 2,
                "Type": 12,
                "AvailableOperant": 289,
                "Description": "Product SKU",
                "Name": "Product SKU",
                "Id": 67
            },
            "Variable2": {
                "$id": "3",
                "$type": "Payback.TradeTool.Entities.Model.Offer.OfferRuleConstant`1[[System.String, mscorlib]], Payback.TradeTool.Entities",
                "Value": "23",
                "Type": 9,
                "AvailableOperant": 289,
                "Description": "2323",
                "Name": "2323",
                "Id": 132
            },
            "Operation": 1,
            "Type": 130,
            "AvailableOperant": 0,
            "Description": "23",
            "Name": "23",
            "Id": 133
        },
        "Value": 23,
        "Id": 16,
        "Type": 0,
        "ValueX": null
    }
],
"Constants": [
    {
        "$id": "4",
        "Value": "rty",
        "Type": 9,
        "AvailableOperant": 289,
        "Description": "rty",
        "Name": "yurt",
        "Id": 130
    },
    {
        "$id": "5",
        "Value": 23,
        "Type": 17,
        "AvailableOperant": 231,
        "Description": "23",
        "Name": "ewe",
        "Id": 131
    },
    {
        "$ref": "3"
    }
],
"BooleanExpressions": [
    {
        "$ref": "1"
    }
],
}

当我使用Constants $id发回4时,5不在解析数据中。但是,如果我在其他属性中引用它们,那么它们也在解析。

修改 下面是我的班级结构

public class OfferDetails
{
    public OfferDetails()
    {
        GeneralDetails = new OfferGeneralDetails();
        TargetDetails = new OfferTargetDetails();
        PointsDetails = new List<OfferPointsDetails>();
        Constants = new List<OfferRuleConstant>();
        BooleanExpressions = new List<OfferRuleBooleanExpression>();
        Variables = new Dictionary<string, List<OfferRulePreDefinedVariable>>();
    }
    public OfferGeneralDetails GeneralDetails { get; set; }
    public OfferTargetDetails TargetDetails { get; set; }
    [JsonProperty(ItemTypeNameHandling = TypeNameHandling.All)]
    public List<OfferPointsDetails> PointsDetails { get; set; }
    [JsonProperty(IsReference = false, ItemTypeNameHandling = TypeNameHandling.All)]
    public List<OfferRuleConstant> Constants { get; set; }
    [JsonProperty(ItemTypeNameHandling = TypeNameHandling.All,IsReference = false)]
    public List<OfferRuleBooleanExpression> BooleanExpressions { get; set; }
    public Dictionary<string, List<OfferRulePreDefinedVariable>> Variables { get; set; }
}


[JsonObject(ItemTypeNameHandling = TypeNameHandling.All, IsReference = true)]
public class OfferRuleConstant<T> : OfferRuleConstant
{
    public OfferRuleConstant()
    {
        Type = RuleVariableType.Constant;
    }
    public T Value {  get => (T)_value; set=> _value = value;}
    public override string ToString()
    {
        return Value.ToString();
    }
}
public abstract class OfferRuleConstant : OfferRuleVariable, ISeprateStringReprsentation
{
    protected object _value;

    public string StringReprsentation { get ; set ; }
}

[JsonObject(IsReference = true, ItemTypeNameHandling = TypeNameHandling.All)]
public class OfferRuleVariable : NamedEntity<long>
{
    public OfferRuleVariable()
    {
        Type = RuleVariableType.Variable;
    }
    public RuleVariableType Type { get; set; }
    public RuleOperantEnum AvailableOperant { get; set; }
    public string Description { get; set; }
    // public int GroupId { get; set; }
}
[JsonObject(IsReference = true, ItemTypeNameHandling = TypeNameHandling.All)]
public class OfferRulePreDefinedVariable : OfferRuleVariable
{
    public int GroupId { get; set; }
}
public interface ISeprateStringReprsentation 
{
    string StringReprsentation { get; set; }
}

我可能在模型中做了一些更改(在几个类中添加了一个新属性,并且在尝试修复上述问题时也更改了一些属性)。

所以,这是我正在尝试解析的更新JSON:

{
   "$id":0,
   "GeneralDetails":{
      "$id":1,
      "ScheduleInfo":{
         "$id":2,
         "ScheduleFrequency":701,
         "ScheduledDay":null,
         "ScheduledMonth":null,
         "ScheduledTime":null,
         "ScheduledTimeS":null
      },
      "Name":"tuy",
      "Description":"ty"
   },
   "BooleanExpressions":[
      {
         "$id":3,
         "$type":"Payback.TradeTool.Entities.Model.Offer.OfferRuleBooleanExpression, Payback.TradeTool.Entities",
         "Type":130,
         "Variable1":{
            "$id":4,
            "Id":46,
            "Name":"Transaction DateTime",
            "Type":100,
            "AvailableOperant":55,
            "Description":"Transaction DateTime"
         },
         "Variable2":{
            "$id":5,
            "$type":"Payback.TradeTool.Entities.Model.Offer.OfferRuleConstant`1[[System.DateTime, mscorlib]], Payback.TradeTool.Entities",
            "Type":97,
            "Value":"2018-04-19T10:36:02.000Z",
            "Name":"6",
            "AvailableOperant":7,
            "Description":"6",
            "StringReprsentation":"4/19/2018, 4:06:02 PM"
         },
         "Operation":1,
         "AvailableOperant":224,
         "Name":"tuy",
         "Description":"uy",
         "StringReprsentation":"(Transaction DateTime EqualTo 4/19/2018, 4:06:02 PM)"
      },
      {
         "$id":6,
         "$type":"Payback.TradeTool.Entities.Model.Offer.OfferRuleBooleanExpression, Payback.TradeTool.Entities",
         "Type":130,
         "Variable1":{
            "$id":7,
            "Id":48,
            "Name":"Transaction Month",
            "Type":4100,
            "AvailableOperant":57,
            "Description":"Transaction Month"
         },
         "Variable2":{
            "$id":8,
            "$type":"Payback.TradeTool.Entities.Model.Offer.OfferRuleListConstant`1[[System.Int32, mscorlib]], Payback.TradeTool.Entities",
            "Type":5649,
            "Value":[
               2,
               4,
               3
            ],
            "Name":"13",
            "AvailableOperant":40,
            "Description":"33",
            "StringReprsentation":"February,April,March"
         },
         "Operation":8,
         "AvailableOperant":224,
         "Name":"tyu",
         "Description":"yu",
         "StringReprsentation":"(Transaction Month In February,April,March)"
      }
   ],
   "TargetDetails":{
      "$id":9,
      "Period":701,
      "Type":2701,
      "Targets":[
         {
            "$id":10,
            "TierId":34,
            "Value":1
         },
         {
            "$id":11,
            "TierId":86,
            "Value":2
         },
         {
            "$id":12,
            "TierId":87,
            "Value":3
         },
         {
            "$id":13,
            "TierId":88,
            "Value":4
         }
      ]
   },
   "PointsDetails":[
      {
         "$id":14,
         "BooleanExperession":{
            "$ref":"3"
         },
         "Type":1,
         "Value":45,
         "ValueX":4
      }
   ],
   "Constants":[
      {
         "$id":15,
         "$type":"Payback.TradeTool.Entities.Model.Offer.OfferRuleConstant`1[[System.Int32, mscorlib]], Payback.TradeTool.Entities",
         "Type":17,
         "Value":"8",
         "Name":"8",
         "AvailableOperant":231,
         "Description":"8",
         "StringReprsentation":"8"
      },
      {
         "$id":16,
         "$type":"Payback.TradeTool.Entities.Model.Offer.OfferRuleListConstant`1[[System.Int32, mscorlib]], Payback.TradeTool.Entities",
         "Type":263697,
         "Name":"2",
         "AvailableOperant":40,
         "Value":[
            1201,
            1202
         ],
         "Description":"2",
         "StringReprsentation":"Male,Female"
      },
      {
         "$id":17,
         "Type":1049617,
         "Value":1501,
         "StringReprsentation":"Dealer",
         "$type":"Payback.TradeTool.Entities.Model.Offer.OfferRuleConstant`1[[System.Int32, mscorlib]], Payback.TradeTool.Entities",
         "Description":"2",
         "Name":"1"
      },
      {
         "$ref":"8"
      },
      {
         "$id":18,
         "$type":"Payback.TradeTool.Entities.Model.Offer.OfferRuleRangeConstant`1[[System.Int32, mscorlib]], Payback.TradeTool.Entities",
         "Type":5393,
         "Value":{
            "$id":19,
            "From":1,
            "To":6
         },
         "Name":"12",
         "AvailableOperant":16,
         "Description":"12",
         "StringReprsentation":"January AND June"
      },
      {
         "$id":20,
         "$type":"Payback.TradeTool.Entities.Model.Offer.OfferRuleConstant`1[[System.Int32, mscorlib]], Payback.TradeTool.Entities",
         "Type":5137,
         "Value":2,
         "Name":"11",
         "AvailableOperant":33,
         "Description":"11",
         "StringReprsentation":"February"
      },
      {
         "$id":21,
         "$type":"Payback.TradeTool.Entities.Model.Offer.OfferRuleListConstant`1[[System.Int32, mscorlib]], Payback.TradeTool.Entities",
         "Type":3601,
         "Value":[
            1,
            2,
            3,
            0
         ],
         "Name":"10",
         "AvailableOperant":40,
         "Description":"33",
         "StringReprsentation":"Tuesday,Wednesday,Thursday,Monday"
      },
      {
         "$id":22,
         "$type":"Payback.TradeTool.Entities.Model.Offer.OfferRuleRangeConstant`1[[System.Int32, mscorlib]], Payback.TradeTool.Entities",
         "Type":3345,
         "Value":{
            "$id":23,
            "From":0,
            "To":4
         },
         "Name":"9",
         "AvailableOperant":231,
         "Description":"9",
         "StringReprsentation":"Monday AND Friday"
      },
      {
         "$id":24,
         "$type":"Payback.TradeTool.Entities.Model.Offer.OfferRuleConstant`1[[System.Int32, mscorlib]], Payback.TradeTool.Entities",
         "Type":3089,
         "Value":0,
         "Name":"7",
         "AvailableOperant":225,
         "Description":"7",
         "StringReprsentation":"Monday"
      },
      {
         "$ref":"5"
      },
      {
         "$id":25,
         "$type":"Payback.TradeTool.Entities.Model.Offer.OfferRuleConstant`1[[System.DateTime, mscorlib]], Payback.TradeTool.Entities",
         "Type":33,
         "Value":"2018-04-04T10:36:02.000Z",
         "Name":"5",
         "AvailableOperant":7,
         "Description":"5",
         "StringReprsentation":"4/4/2018"
      },
      {
         "$id":26,
         "$type":"Payback.TradeTool.Entities.Model.Offer.OfferRuleConstant`1[[System.Int32, mscorlib]], Payback.TradeTool.Entities",
         "Type":17,
         "Value":"4",
         "Name":"4",
         "AvailableOperant":231,
         "Description":"4",
         "StringReprsentation":"4"
      },
      {
         "$id":27,
         "$type":"Payback.TradeTool.Entities.Model.Offer.OfferRuleConstant`1[[System.String, mscorlib]], Payback.TradeTool.Entities",
         "Type":9,
         "Name":"3",
         "AvailableOperant":289,
         "Value":"3",
         "Description":"3",
         "StringReprsentation":"3"
      }
   ],
   "Variables":{
      "$id":28
   }
}

大多数常量都在解析,但对应于"$id":17不在列表中。

1 个答案:

答案 0 :(得分:0)

经过几次尝试,我找到了我的错误,在json中是"$id"属性。这是数字,但是从API生成的原始JSON中,它是数字。

所以,我改变了生成JSON的javaScript代码并且它有效。但是我无法理解它失败的原因。当我最初使用与生成的表单API相同的数据发布时。可能我只是感到困惑。