0

How do I create a JSON array using Newtonsoft.JSON (json.net) from this json string

[
  {
    "Cells": {
      "results": [
        {
          "Key": "Title",
          "Value": "hello",
          "ValueType": "Edm.String"
        },
        {
          "Key": "Size",
          "Value": "54549",
          "ValueType": "Edm.Int64"
        },
        {
          "Key": "Path",
          "Value": "http://somesite/a/hello.pptx",
          "ValueType": "Edm.String"
        },
        {
          "Key": "Summary",
          "Value": "Some summary <ddd/> interesting reading <ddd/> nice book <ddd/> ",
          "ValueType": "Edm.String"
        },
        {
          "Key": "Name",
          "Value": "http://somesite",
          "ValueType": "Edm.String"
        }
      ]
    }
  },
  {
    "Cells": {
      "results": [
        {
          "Key": "Title",
          "Value": "hi joe",
          "ValueType": "Edm.String"
        },
        {
          "Key": "Size",
          "Value": "41234",
          "ValueType": "Edm.Int64"
        },
        {
          "Key": "Path",
          "Value": "http://someothersite/interesting/hi.pptx",
          "ValueType": "Edm.String"
        },
        {
          "Key": "Summary",
          "Value": "Some summary <ddd/> interesting reading <ddd/> nice book <ddd/> ",
          "ValueType": "Edm.String"
        },
        {
          "Key": "Name",
          "Value": "http://somesite",
          "ValueType": "Edm.String"
        }
      ]
    }
  }
]

json2csharp gives me following classes for this structure

public class Result
{
    public string Key { get; set; }
    public string Value { get; set; }
    public string ValueType { get; set; }
}

public class Cells
{
    public List<Result> results { get; set; }
}

public class RootObject
{
    public Cells Cells { get; set; }
}

How do I use these classes to create json array?

UPDATE AND SOLUTION

this will work

static void Main(string[] args)
{
    RootObject ro = new RootObject();
    Cells cs = new Cells();
    cs.results = new List<Result>();

    Result rt = new Result();
    rt.Key = "Title";
    rt.Value = "hello";
    rt.ValueType = "Edm.String";
    cs.results.Add(rt);

    Result rs = new Result();
    rs.Key = "Size";
    rs.Value = "3223";
    rs.ValueType = "Edm.Int64";
    cs.results.Add(rs);

    ro.Cells = cs;

    string json = JsonConvert.SerializeObject(ro);
}
3
  • figured it out, see solution above. Thanks Commented Jun 1, 2017 at 19:54
  • 2
    Rather than including the answer in your question, you could answer your own question and accept the answer so the rest of us can tell your question is resolved. Commented Jun 1, 2017 at 20:00
  • Thanks, updated to answer Commented Jun 1, 2017 at 20:10

4 Answers 4

2

this will work

static void Main(string[] args)
{
    RootObject ro = new RootObject();
    Cells cs = new Cells();
    cs.results = new List<Result>();

    Result rt = new Result();
    rt.Key = "Title";
    rt.Value = "hello";
    rt.ValueType = "Edm.String";
    cs.results.Add(rt);

    Result rs = new Result();
    rs.Key = "Size";
    rs.Value = "3223";
    rs.ValueType = "Edm.Int64";
    cs.results.Add(rs);

    ro.Cells = cs;

    string json = JsonConvert.SerializeObject(ro);
}
Sign up to request clarification or add additional context in comments.

Comments

1

You're looking for the function DeserializeObject<T>:

var json = ""; // string up above in your code
var jObect = JsonConvert.DeserializeObject<RootObject>(json);

// Use

var cells = jObject.Cells;
var result1 = cells.results.FirstOrDefault();

3 Comments

Thanks, but I need to produce json string from my objects, see Update above.
@pixel Going in the opposite direction is var jsonStr = JsonConvert.SerializeObject(myObject).
Actually, what I need next is to convert it back to JArray not jsonString :)
0

Given an example of a POCO below:

    public class Account
{
    public string Email { get; set; }
    public bool Active { get; set; }
    public DateTime CreatedDate { get; set; }
    public IList<string> Roles { get; set; }
}

This can be achieved by deserializing your JSON string show below:

    string json = @"{
   'Email': '[email protected]',
   'Active': true,
   'CreatedDate': '2013-01-20T00:00:00Z',
   'Roles': [
     'User',
     'Admin'
   ]
 }";

Account account = JsonConvert.DeserializeObject<Account>(json);

Console.WriteLine(account.Email);

Reference Newtonsoft's documentation below: http://www.newtonsoft.com/json/help/html/DeserializeObject.htm

1 Comment

Thanks, I dont want to use json string. I am providing json string above just to show the structure.
0

If you want the string representation of an object, especially a json object, the most relevant is .ToString (). But, it could fail for other reasons ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.