Click here to Skip to main content
15,846,346 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,
I am having a json like following
["G6863","2010368363","201030821","NewData","Latest"]


I want to Convert the above JSON to c# classs of fallowng
C#
public Class classified{
public string GId{set;get}
public string UID{set;get}
public string UDATE{set;get}
public string MESSAGE{set;get}
public string UPDATEREASON{set;get}
}




i want to be it generic as i need to write it to many files

What I have tried:

so i tried the following

C#
var SerializedText=["G6863","2010368363","201030821","NewData","Latest"]
JsonConvert.DeserializeObject(SerializedText, typeof(classified)) as classified



but it is throwing fallowing error

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'classified' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<t> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.
Posted
Updated 20-Jan-23 0:36am
v2
Comments
Mehdi Gholam 9-Oct-17 2:12am    
An array is not a class, just read the string values and place them in your class yourself.
Jayadeep333 9-Oct-17 2:13am    
is there any way if i convert it json array
Graeme_Grant 9-Oct-17 2:36am    
The error explains it all - it is not valid JSON data. So, is this the complete JSON data? Where is it coming from?
Richard MacCutchan 9-Oct-17 3:41am    
An array of strings is not JSON. You need to write some code to extract the individual items from the array and store them in your properties.

For such of requirements you can use class constructor. See: Constructors (C# Programming Guide) | Microsoft Docs[^]
 
Share this answer
 
You could use something like this:

C#
using System.Text.Json;

namespace TestDictionary;

internal class Program
{
    static void Main(string[] args)
    {
        string response = "[\"G6863\",\"2010368363\",\"201030821\",\"NewData\",\"Latest\"]";
        var deserializedResponse = JsonSerializer.Deserialize<string[]>(response);

        var data = new Classified(deserializedResponse);

        // Do something...
    }
}

public class Classified
{
    public string GId { set; get; }
    public string UID { set; get; }
    public string UDATE { set; get; }
    public string MESSAGE { set; get; }
    public string UPDATEREASON { set; get; }

    public Classified(string[] data)
    {
        GId = data[0];
        UID = data[1];
        UDATE = data[2];
        MESSAGE = data[3];
        UPDATEREASON = data[4];
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900