0

i have two arrays of the same length that i would like to use the contents. index[0] of both array a and b should be used together when i am saving to a json file.i am guessing i need to lopp thru them in order to access each index and save the contents to a template json every time. i also would like that it returns a toString() format of the json, so each iteration for each index returns something.

 public string Show(string[] id, string[] msg)
   {
   // opening code for json file with jobject and jsontexreader
   for (int i = 0; i <= id.Length; i++ )
      {                    
                Newid = id[i];
                Newmsg = msgs[i];
                // setting the data to the json file
                JObject temp = (JObject)o1.SelectToken(path1);
                temp["data"] = msg;
                JObject tem = (JObject)o1.SelectToken(path2);
                tem["ksid"] = id;

        }
       return ??;    

}

Carlos C
  • 23
  • 4
  • 2
    What's your actual question? This is just a list of requirements. – tnw Jun 01 '17 at 16:15
  • Is there any chance that the `msg` length will be different from the length of `id`? – Svek Jun 01 '17 at 16:19
  • Possible duplicate of [How do I create a single list of object pairs from two lists in C#?](https://stackoverflow.com/questions/7110762/how-do-i-create-a-single-list-of-object-pairs-from-two-lists-in-c) – Juan Carlos Martínez Jun 01 '17 at 16:38

1 Answers1

1

Here is a simple example of accomplishing what you wanted to do

string Show(string[] id, string[] msg)
{
    if (id.Length != msg.Length) 
        throw new Exception(nameof(id) + " is not the same length as " + nameof(msg));

    List<object> data = new List<object>();

    for (int i = 0; i < id.Length; i++)
    {
        data.Add(new
        {
            Ksid = id[i],
            Data = msg[i]
        });
    }

    return Newtonsoft.Json.JsonConvert.SerializeObject(data);
}
Svek
  • 12,350
  • 6
  • 38
  • 69
  • how would i be able to add the Ksid and Data into a json file template? say i have a json file of the following that i will put the data and id inside. then after ti copies this json format i want to output the json with th enew info added { "messageR": { "appId": "myApp", "messages": { "message": { "content": { "priorityService": "false", "data": "Test Message", "mimeType": "text/plain" }, } } } } – Carlos C Jun 01 '17 at 17:02
  • yes i tried it, i had to tweak some other things around but thank you. – Carlos C Jun 02 '17 at 13:24