Home > Back-end >  Not able to update a value from an array of JToken Type
Not able to update a value from an array of JToken Type

Time:02-19

I am trying to update a value which is a parameter in an array of JToken type. But I am getting the error below:

Set JArray values with invalid key value: "tagCategoryId". Int32 array index expected.

Below is my method:

JObject _asset;
JToken _tagCategoryObject ;

public void Test1()
    {
        var invalidTagCategoryId = "invalidTagCategoryId";
        while (invalidTagCategoryId.Length <= 255) invalidTagCategoryId  = invalidTagCategoryId;
        _asset.TryGetValue("enumeratedTagCategories", out _tagCategoryObject);
        if (_tagCategoryObject != null)
        {
            _tagCategoryObject["tagCategoryId"] = invalidTagCategoryId;
           
        }
      .....
    }

Below is the json:

"enumeratedTagCategories": [ 
    {
    "tagCategoryId": "TagCategoryId",
    "tagCategoryDisplayName": "TagCategoryDisplayName",
    "version": "Version",
    "tagValueIdsList": [
      {
        "displayName": "DisplayName1"
      },
      {
        "displayName": "DisplayName2"
      }
    ]
  }
  ]

How can I assign a value to tagCategoryId ?

CodePudding user response:

_tagCategoryObject is array so try this

var _tagCategoryObject = JObject.Parse(json)["enumeratedTagCategories"];

if (_tagCategoryObject != null)
{
 _tagCategoryObject[0]["tagCategoryId"] = invalidTagCategoryId;
           
}

and fix you json by wrapping in {}

{ "enumeratedTagCategories": [ 
    {
    "tagCategoryId": "TagCategoryId",
    "tagCategoryDisplayName": "TagCategoryDisplayName",
    "version": "Version",
    "tagValueIdsList": [
      {
        "displayName": "DisplayName1"
      },
      {
        "displayName": "DisplayName2"
      }
    ]
  }
  ]
}
  • Related