Home > Back-end >  C# Json Array without Property Name
C# Json Array without Property Name

Time:09-07

I'm writing a code to serialize JSON from the class:

public class ActorRiskAssessments
{        
    public string asOfDate { get; set; }
    public string type { get; set; }     
    public string value { get; set; }
    [JsonIgnore]
    public string id { get; set; }
    [JsonIgnore]
    public string pathParameter { get; set; }
} 

I want to generate a JSON out of this class to be like:

[ { "asOfDate": "2019-08-24", "type": "INTERNAL", "value": "string" } ]

As you can see above, I don't have the array's property name in JSON array. I don't know how to achieve this. This is my current code for the assigning:

            List<ActorRiskAssessments> actors = new List<ActorRiskAssessments>();

            actors = (from DataRow dr in dataTable.Rows
                      select new ActorRiskAssessments()
                      {   
                          id = dr["Actor.Id"].ToString(),
                          asOfDate = DateTime.Now.ToString("yyyy-MM-dd"),
                          type = dr["Actor.type"].ToString(),
                          value = dr["Actor.value"].ToString(),
                          pathParameter = lucinityURL   dr["pathParameter"].ToString()                             
                      }
                     ).ToList();

            cnn.Open();
            
            using (SqlCommand insertCommand = new SqlCommand(insertQuery, cnn))
            {

                foreach (var serializer in actors)
                {
                    json = JsonSerializer.Serialize(serializer);
                    dtActorRiskAssessments.Rows.Add(new object[] { targetObject, serializer.id, json, serializer.pathParameter }); // add rows to DataTable                                                                                                                                 
                }

                var param = insertCommand.Parameters.AddWithValue("@ActorRiskAssessments", dtActorRiskAssessments);
                param.TypeName = "export.CorrespondenceTableType"; //DataType is required to register on MSSQL Site look a database project for this type
                insertCommand.ExecuteNonQuery();
                insertCommand.Parameters.Clear();
            }

CodePudding user response:

try changing

 foreach (var serializer in actors)
 {
    json = JsonSerializer.Serialize(serializer);
   //Continuation of previous codes  ...                                                                                                                          
  }

to

foreach (var serializer in actors)
{
   json = JsonSerializer.Serialize(new List<ActorRiskAssessments>() { serializer });
   //Continuation of previous codes  ...                                                                                                                                 
}

The above code serializes each member of the list as an array. If you need to serialize the whole list at once, you don't need a loop like below

 var result = JsonSerializer.Serialize(actors);
  • Related