0

I'm using RestSharp in my app and I need to iterate some part in a code.

request.AddParameter("undefined", "{\r\n    \"targetType\": \"SYSTEM\",\r\n    \"name\": \"xxxxxxxxxx\",\r\n    \"message\": \"Daily usage exceeded\",\r\n    \"active\": true,\r\n    \"emails\": [\"machin@truc.com\"],\r\n    \"conditions\": [{\r\n        \"operator\": \"EQUALS\",\r\n        \"operands\": [\r\n            {\r\n                \"attributeId\": \r\n                {\r\n                    \"name\": \"system.usage.status\"\r\n                }\r\n            \r\n            }, \r\n            {\r\n                \"valueStr\": \"nearing plan\"\r\n            }\r\n        ]\r\n    },\r\n    {\r\n        \"operator\": \"EQUALS\",\r\n        \"operands\": [\r\n            {\r\n                \"attributeId\": \r\n                {\r\n                    \"name\": \"system.name\"\r\n                }\r\n            \r\n            }, \r\n            {\r\n                \"valueStr\": \"SIM 89332401000006726968\"\r\n               \r\n            }\r\n        ]\r\n    },\r\n     {\r\n        \"operator\": \"EQUALS\",\r\n        \"operands\": [\r\n            {\r\n                \"attributeId\": \r\n                {\r\n                    \"name\": \"system.name\"\r\n                }\r\n            \r\n            }, \r\n            {\r\n                \"valueStr\": \"SIM 89332401000006726968\"\r\n               \r\n            }\r\n        ]\r\n    }\r\n    \r\n    ]\r\n}", ParameterType.RequestBody);

How can I iterate that part with foreach?

 {\r\n        \"operator\": \"EQUALS\",\r\n        \"operands\": [\r\n            {\r\n                \"attributeId\": \r\n                {\r\n                    \"name\": \"system.name\"\r\n                }\r\n            \r\n            }, \r\n            {\r\n                \"valueStr\": \"SIM 89332401000006726968\"\r\n               \r\n            }\r\n        ]\r\n    }\r\n 
anonn
  • 1
  • That looks like json. So de-serialze it to a proper List of objects and then you can loop them. – VDWWD Jul 25 '18 at 09:29
  • What? _"iterate that part with foreach"_? For each char? For each Key, value in a dictionary build that string? You wan to convert it to a `@""` so the `\rn` wont be needed? Iterate on the Properties of an unknow object using reflexion? It's unclear. use as many word as you can to descript what you want. – Drag and Drop Jul 25 '18 at 09:29
  • Could be a duplicate of https://stackoverflow.com/questions/4611031/convert-json-string-to-c-sharp-object. – Drag and Drop Jul 25 '18 at 09:36

1 Answers1

1

Create class which represents your json structure, take some help from http://json2csharp.com/

public class AttributeId
{
    public string name { get; set; }
}

public class Operand
{
    public AttributeId attributeId { get; set; }
    public string valueStr { get; set; }
}

public class Condition
{
    public string @operator { get; set; }
    public List<Operand> operands { get; set; }
}

public class RootObject
{
    public string targetType { get; set; }
    public string name { get; set; }
    public string message { get; set; }
    public bool active { get; set; }
    public List<string> emails { get; set; }
    public List<Condition> conditions { get; set; }
}

After that,

var myJsonObject = JsonConvert.DeserializeObject<RootObject>(yourJsonString);

and then iterate on myJsonObject.conditions

Emre Savcı
  • 3,034
  • 2
  • 16
  • 25