Click here to Skip to main content
15,742,477 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Below is a json string
   "attributes": {
    "TRACKNAME": "A1",
    "LRS_SEGMENT": "A11",
    "FROM_CHM": 0.0,
    "TO_CHM": 3425.13
   "attributes": {
    "TRACKNAME": "A1",
    "LRS_SEGMENT": "A12",
    "FROM_CHM": 3382.14,
    "TO_CHM": 8022.48
   "attributes": {
    "TRACKNAME": "A1",
    "LRS_SEGMENT": "A13",
    "FROM_CHM": 7980.12,
    "TO_CHM": 8845.55}

I created such 2 classes as below:
public class LRSSegment {
    public string TRACKNAME { get; set;}
    public int SEGSEQUENCE { get; set; }
    public string LRS_SEGMENT { get; set; }
    public double FROM_CHM { get; set; }
    public double TO_CHM { get; set; }

public class LRSAttribues
    public IList<LRSSegment>lrsAttribute { get; set; }

Then I want to convert the json string into a json object through this line of code
LRSAttribues lrsSeg = JsonSerializer.Deserialize<LRSAttribues>(jString); 
 // Got error

However, I got error. What's wrong in my code? Thanks if you can point it out.

What I have tried:

How to Convert a json string to json object in net6?
Updated 12-May-23 4:12am

1 solution

I got error
And you didn't think it was important to tell us what error you got?

Your JSON represents a single object with a property called features. That property contains an array of objects, each of which contains a property called attributes. That property contains a single object which seems to match your LRSSegment class.

To deserialize that, you need your C# classes to match the JSON structure:
public class Root
    public Feature[] Features { get; set; }

public partial class Feature
    public LRSSegment Attributes { get; set; }
Root lrsSeg = JsonSerializer.Deserialize<Root>(jString);

If in doubt, there are plenty of tools you can use to generate C# classes from JSON - for example,[^].

Visual Studio also has a "paste special" option to "paste JSON as classes":
Paste JSON or XML as classes - Visual Studio (Windows) | Microsoft Learn[^]
Share this answer
s yu 12-May-23 10:24am    
Richard: Thanks for your quick response. I got error on [JsonProperty...] - Not an attribute class.
I use VS2022, net6. Could you tell how to fix this error?
Richard Deeming 12-May-23 10:26am    
For System.Text.Json, use [JsonPropertyName("...")] instead of [JsonProperty("...")].
s yu 12-May-23 15:25pm    
Richard: Thank you so much. Using VS "paste special" tool, the classes are well generated. But I got another problem in converting Int32. I use such piece of code as:
var serializeOperations = new JsonSerializerOptions();
serializeOperations.Converters.Add(new Int32Converter());
//Error CS1503 Argument 1: cannot convert from 'System.ComponentModel.Int32Converter' to 'System.Text.Json.Serialization.JsonConverter'

Root lrsSeg = JsonSerializer.Deserialize<root>(result, serializeOperations);
I have searched around but have not got the answer yet. What's your advisory? Thanks again.
Richard Deeming 15-May-23 3:15am    
Int32Converter[^] is a TypeConverter; the JsonSerializerOptions.Converters collection is a collection of JsonConverter[^] instances.

A TypeConverter is not a JsonConverter, and cannot be added to a collection of JsonConverters.

And why would you need to? The JsonSerializer already knows how to handle numbers, including Int32/int.

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