SendMatchState..send an array


I’m going crazy on a C # topic but maybe you can help me.
In a match I have to send an array of data.

Imagine something like this:

pos x = 10 y = 20 z = 30
pos x = 15 y = 34 z = 10
pos …

This data is sent by each player: indeed each player has many positions.

Looking at Fish’s example the result is easy: data is sent but it is not an array


var values = new Dictionary <string, string>
             {"velocity.x", velocity.x.ToString ()},
             {"velocity.y", velocity.y.ToString ()},
             {"position.x", position.x.ToString ()},
             {"position.y", position.y.ToString ()}

         return values.ToJson ();

Could you help me?
thank you

I’m not sure I understood the problem you’re having, add the positions to a List and set it as one of the Dictionary values before marshaling and you should be able to send them as JSON.

Hi @sesposito ,

I try to clarify it better.
Each player has 3 or more sub-objects which have their position in the game space.

The first way I thought is to create a dictionary with a “count” key that contains the number of sub-objects and then create as many “position” keys as there are objects.
This solution works but I wanted to understand if there is something more elegant like the use of arrays in JSON.


var values = new Dictionary <string, string>
             {"count.”, items.count.ToString ()},
             {"Position1", items[0].position.x.ToString ()},
             {"Position2", items[1].position.x ToString ()},

Thank you

You need to create a class to represent the json to be serialized, and use a list or an array type to represent your positions, e.g.:

using System;
using System.Collections.Generic;
using System.Text.Json;

class Program {
  public static void Main (string[] args) {
    var pr = new PositionsRepresenter{
      Positions = new List<string>(){"x,y,z", "x,y,z", "x,y,z"}

    var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
    string jsonString = JsonSerializer.Serialize(pr, options);

public class PositionsRepresenter {
  public IList<string> Positions { get; set; }

Output: {"positions":["x,y,z","x,y,z","x,y,z"]}

See: How to serialize and deserialize JSON using C# - .NET | Microsoft Docs