Search code examples
jsonactivexobjectchilkat

Chilkat - Generating single Json array from two arrays


Lets say you have the following Chilkat JsonObject named jsonA:

"object": "list",
"data": [
 {0},
 {1},
 {2},
 {3},
 {4},
 {5}
],
"has_more": true
}

Lets say you have the identical following JsonObject named jsonB:

"object": "list",
"data": [
  {6},
  {7},
  {8}
  ],
  "has_more": false
  }

Utilizing the bundle, what would be the best approach to generate a single "data" array of:

"data": [
{0},
{1},
{2},
{3},
{4},
{5},
{6},
{7},
{8}
],

I've dug through the Json reference documentation and cannot seem to find a method that would do this? Each array can contain up to 100 items, so I would rather not have to loop through each item if possible.


Solution

  • Here's what I would do..

    First.. the syntax "{0}" doesn't make sense to me. If the array item begins with "{", then it should contain a name/value pair such as: "name" : value

                string strA = @"
    {
    ""object"": ""list"",
    ""data"": [ 0,1,2,3,4,5 ],
    ""has_more"": true
    }";
                string strB = @"
    {
    ""object"": ""list"",
    ""data"": [ 6,7,8 ],
    ""has_more"": false
    }";
    
            Chilkat.JsonObject jsonA = new Chilkat.JsonObject();
            jsonA.Load(strA);
    
            Chilkat.JsonObject jsonB = new Chilkat.JsonObject();
            jsonB.Load(strB);
    
            Chilkat.JsonArray a = jsonA.ArrayOf("data");
    
            int numDataItems = jsonB.SizeOfArray("data");
            int i;
            for (i=0; i<numDataItems; i++)
                {
                jsonB.I = i;
                a.AddIntAt(-1,jsonB.IntOf("data[i]"));
                }
    
            jsonA.EmitCompact = false;
            textBox1.Text = jsonA.Emit();