Skip to main content
deleted 14 characters in body
Source Link

You get the JSON result as a string, so it might be easier for you to remove all the numbers from valuetoExtract. For that you could use Regex.Replace() from System.Text.RegularExpressions namespace. Then you can use a single property for valuetoExtract key.

So your code can look like this:

var jsonString = "{ 'option1': 'someOption1','option2': 'someOption2','id': ['12345'],'filter': 'someFilter','actualValues': [{'otherId': 24,'id': 12345,'valuetoExtract129': '123'},{'otherId': 24,'id': 12345,'valuetoExtract2': '234'},{'otherId': 24,'id': 12345,'valuetoExtract3': '345'}]}";

jsonString = Regex.Replace(jsonString, @"valuetoExtract[0-9]*", "valuetoExtract");
ApiResult result = JsonConvert.DeserializeObject<ApiResult>(jsonString);

Where ApiResult is defined as the following

class ApiResult
{
    [JsonProperty(propertyName: "option1")]
    public string Option1 { get; set; }

    [JsonProperty(propertyName: "option2")]
    public string Option2 { get; set; }

    [JsonProperty(propertyName: "id")]
    public string[] Id { get; set; }

    [JsonProperty(propertyName: "filterd")]
    public string Filter { get; set; }

    [JsonProperty(propertyName: "actualValues")]
    public ICollection<ActualValue>ActualValue[] ActualValues { get; set; }
}

class ActualValue
{
    [JsonProperty(propertyName: "id")]
    public int Id { get; set; }

    [JsonProperty(propertyName: "otherId")]
    public int OtherId { get; set; }

    [JsonProperty(propertyName: "valuetoExtract")]
    public stringint ValueToExtract { get; set; }
}

You get the JSON result as a string, so it might be easier for you to remove all the numbers from valuetoExtract. For that you could use Regex.Replace() from System.Text.RegularExpressions namespace. Then you can use a single property for valuetoExtract key.

So your code can look like this:

var jsonString = "{ 'option1': 'someOption1','option2': 'someOption2','id': ['12345'],'filter': 'someFilter','actualValues': [{'otherId': 24,'id': 12345,'valuetoExtract129': '123'},{'otherId': 24,'id': 12345,'valuetoExtract2': '234'},{'otherId': 24,'id': 12345,'valuetoExtract3': '345'}]}";

jsonString = Regex.Replace(jsonString, @"valuetoExtract[0-9]*", "valuetoExtract");
ApiResult result = JsonConvert.DeserializeObject<ApiResult>(jsonString);

Where ApiResult is defined as the following

class ApiResult
{
    [JsonProperty(propertyName: "option1")]
    public string Option1 { get; set; }

    [JsonProperty(propertyName: "option2")]
    public string Option2 { get; set; }

    [JsonProperty(propertyName: "id")]
    public string[] Id { get; set; }

    [JsonProperty(propertyName: "filterd")]
    public string Filter { get; set; }

    [JsonProperty(propertyName: "actualValues")]
    public ICollection<ActualValue> ActualValues { get; set; }
}

class ActualValue
{
    [JsonProperty(propertyName: "id")]
    public int Id { get; set; }

    [JsonProperty(propertyName: "otherId")]
    public int OtherId { get; set; }

    [JsonProperty(propertyName: "valuetoExtract")]
    public string ValueToExtract { get; set; }
}

You get the JSON result as a string, so it might be easier for you to remove all the numbers from valuetoExtract. For that you could use Regex.Replace() from System.Text.RegularExpressions namespace. Then you can use a single property for valuetoExtract key.

So your code can look like this:

var jsonString = "{ 'option1': 'someOption1','option2': 'someOption2','id': ['12345'],'filter': 'someFilter','actualValues': [{'otherId': 24,'id': 12345,'valuetoExtract129': '123'},{'otherId': 24,'id': 12345,'valuetoExtract2': '234'},{'otherId': 24,'id': 12345,'valuetoExtract3': '345'}]}";

jsonString = Regex.Replace(jsonString, @"valuetoExtract[0-9]*", "valuetoExtract");
ApiResult result = JsonConvert.DeserializeObject<ApiResult>(jsonString);

Where ApiResult is defined as the following

class ApiResult
{
    [JsonProperty(propertyName: "option1")]
    public string Option1 { get; set; }

    [JsonProperty(propertyName: "option2")]
    public string Option2 { get; set; }

    [JsonProperty(propertyName: "id")]
    public string[] Id { get; set; }

    [JsonProperty(propertyName: "filterd")]
    public string Filter { get; set; }

    [JsonProperty(propertyName: "actualValues")]
    public ActualValue[] ActualValues { get; set; }
}

class ActualValue
{
    [JsonProperty(propertyName: "id")]
    public int Id { get; set; }

    [JsonProperty(propertyName: "otherId")]
    public int OtherId { get; set; }

    [JsonProperty(propertyName: "valuetoExtract")]
    public int ValueToExtract { get; set; }
}
added code sample
Source Link

You could do something weirdget the JSON result as replacinga string, so it might be easier for you to remove all matching substrings ofthe numbers from "valuetoExtract{X}": with justvaluetoExtract. For that you could use "valuetoExtract": thenRegex.Replace() from System.Text.RegularExpressions namespace. Then you can use a single object to parse it intoproperty for valuetoExtract key.

So your code can look like this:

var jsonString = "{ 'option1': 'someOption1','option2': 'someOption2','id': ['12345'],'filter': 'someFilter','actualValues': [{'otherId': 24,'id': 12345,'valuetoExtract129': '123'},{'otherId': 24,'id': 12345,'valuetoExtract2': '234'},{'otherId': 24,'id': 12345,'valuetoExtract3': '345'}]}";

jsonString = Regex.Replace(jsonString, @"valuetoExtract[0-9]*", "valuetoExtract");
ApiResult result = JsonConvert.DeserializeObject<ApiResult>(jsonString);

Where ApiResult is defined as the following

class ApiResult
{
    [JsonProperty(propertyName: "option1")]
    public string Option1 { get; set; }

    [JsonProperty(propertyName: "option2")]
    public string Option2 { get; set; }

    [JsonProperty(propertyName: "id")]
    public string[] Id { get; set; }

    [JsonProperty(propertyName: "filterd")]
    public string Filter { get; set; }

    [JsonProperty(propertyName: "actualValues")]
    public ICollection<ActualValue> ActualValues { get; set; }
}

class ActualValue
{
    [JsonProperty(propertyName: "id")]
    public int Id { get; set; }

    [JsonProperty(propertyName: "otherId")]
    public int OtherId { get; set; }

    [JsonProperty(propertyName: "valuetoExtract")]
    public string ValueToExtract { get; set; }
}

You could do something weird as replacing all matching substrings of "valuetoExtract{X}": with just "valuetoExtract": then use a single object to parse it into.

You get the JSON result as a string, so it might be easier for you to remove all the numbers from valuetoExtract. For that you could use Regex.Replace() from System.Text.RegularExpressions namespace. Then you can use a single property for valuetoExtract key.

So your code can look like this:

var jsonString = "{ 'option1': 'someOption1','option2': 'someOption2','id': ['12345'],'filter': 'someFilter','actualValues': [{'otherId': 24,'id': 12345,'valuetoExtract129': '123'},{'otherId': 24,'id': 12345,'valuetoExtract2': '234'},{'otherId': 24,'id': 12345,'valuetoExtract3': '345'}]}";

jsonString = Regex.Replace(jsonString, @"valuetoExtract[0-9]*", "valuetoExtract");
ApiResult result = JsonConvert.DeserializeObject<ApiResult>(jsonString);

Where ApiResult is defined as the following

class ApiResult
{
    [JsonProperty(propertyName: "option1")]
    public string Option1 { get; set; }

    [JsonProperty(propertyName: "option2")]
    public string Option2 { get; set; }

    [JsonProperty(propertyName: "id")]
    public string[] Id { get; set; }

    [JsonProperty(propertyName: "filterd")]
    public string Filter { get; set; }

    [JsonProperty(propertyName: "actualValues")]
    public ICollection<ActualValue> ActualValues { get; set; }
}

class ActualValue
{
    [JsonProperty(propertyName: "id")]
    public int Id { get; set; }

    [JsonProperty(propertyName: "otherId")]
    public int OtherId { get; set; }

    [JsonProperty(propertyName: "valuetoExtract")]
    public string ValueToExtract { get; set; }
}
Source Link

You could do something weird as replacing all matching substrings of "valuetoExtract{X}": with just "valuetoExtract": then use a single object to parse it into.