1

I have done this.

JObject jsonGet = new JObject();
jsonGet.Add("VariableName1", "temp1" );
jsonGet.Add("VariableName2", "temp2" );
jsonGet.Add("VariableName3", temp3" );
restRequest.AddParameter("application/json", jsonGet, ParameterType.RequestBody);

And I have something like this.

{
  "VariableName1": "temp1",
  "VariableName2": "temp2",
  "VariableName3": "temp3",
}

And I would like to do this.

{
  "VariableList1": 
  [
    {
      "VariableName1": temp1,
      "VariableName2": "temp2",
      "VariableName3": "temp3"
    },
    {
      "VariableName1": temp1,
      "VariableName2": "temp2",
      "VariableName3": "temp3"
    },
    {
      "VariableName1": temp1,
      "VariableName2": "temp2",
      "VariableName3": "temp3"
    },
    "VariableList2": [
    {
      "VariableName1": temp1,
      "VariableName2": "temp2",
      "VariableName3": "temp3"
    },
    {
      "VariableName1": temp1,
      "VariableName2": "temp2",
      "VariableName3": "temp3"
    },
    {
      "VariableName1": temp1,
      "VariableName2": "temp2",
      "VariableName3": "temp3"
    }
  ]
}

And I want to do this like above with the JsonObject.

I want a json which will contains a header "Variablelist1" and a header "VariableList2" and the items in each will have the same names

which is the code in c# for this?

7
  • The JSON you posted for what you want is invalid - I suspect you meant to close the Variable1List array, but I haven't changed it. Please edit it to be valid JSON. But basically, you'll want to use JArray to create arrays. See newtonsoft.com/json/help/html/… for some examples. Commented Dec 7, 2019 at 8:04
  • It's also not clear to me what this has to do with the parsing that's mentioned in your title. Perhaps edit the title to ask about "creating arrays" or similar? Commented Dec 7, 2019 at 8:08
  • @JonSkeet, Edit what you thing it needs because i dont know how to do it, yes basically i want a json which will contains a header "Variablelist1" and a header "VariableList2" and the items in each will have the same names. Commented Dec 7, 2019 at 8:11
  • Like "Sender": name, lastname, age and "Receiver": name, lastname, age. Commented Dec 7, 2019 at 8:15
  • @JonSkeet, anyone that can help?? Commented Dec 7, 2019 at 9:00

1 Answer 1

2

You could model your data as classes and use these as the response and not use the generic JSON classes directly.

public class Variables {
    public string VariableName1 { get; set; }
    public string VariableName2 { get; set; }
    public string VariableName3 { get; set; }
}

public class Model {
    public Model() {
        VariableList1 = new List<Variables>();
        VariableList2 = new List<Variables>();
    }

    public List<Variables> VariableList1 { get; set; }
    public List<Variables> VariableList2 { get; set; }
}

See the fiddle for an example. To post the model via restsharp you could take a look into this answer.

Sign up to request clarification or add additional context in comments.

Comments