1

I have and array in C# and I need to insert it in json: I have:

int[] a = new int[3];
JObject something=...;

a[0]=12;
a[1]=65;
a[3]=90;

I need something["numbers"].Value="[12,65,90]"

How can I get this?

1 Answer 1

6

It sounds like you really just want to use JArray to wrap the array:

something["numbers"] = new JArray(a);

In other words, let Json.NET take care of the textual representation - you just need to tell it the logical value, which is just an array of numbers. Here's a short but complete example:

using System;
using Newtonsoft.Json.Linq;

public class Test
{
    public static void Main()
    {
        JObject json = new JObject();
        int[] array = { 1, 2, 3 };
        json["numbers"] = new JArray(array);
        Console.WriteLine(json);
    }
}

Output:

{
  "numbers": [
    1,
    2,
    3
  ]
}
Sign up to request clarification or add additional context in comments.

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.