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?

  • 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 https://www.newtonsoft.com/json/help/html/CreatingLINQtoJSON.htm#CreatingLINQ for some examples. – Jon Skeet Dec 07 '19 at 08: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? – Jon Skeet Dec 07 '19 at 08: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. –  Dec 07 '19 at 08:11
  • Like "Sender": name, lastname, age and "Receiver": name, lastname, age. –  Dec 07 '19 at 08:15
  • @JonSkeet, anyone that can help?? –  Dec 07 '19 at 09:00
  • could anyone help with this issue?? –  Dec 07 '19 at 10:30
  • To edit, just hit the "edit" link. Look very carefully at the JSON, and work out where you'd want the first array to finish. – Jon Skeet Dec 07 '19 at 15:43

1 Answers1

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.

Nico
  • 3,542
  • 24
  • 29