-2

I have some json that looks like this:

"lines": 
{
    "0010": {
        "ProvCountyCode": "047",
        "ProvNo": "770887",
        "ParNonparCode": "K",
        "ParNonparCode2": "K",
    }, 
    "0020" {
        "ProvCountyCode": "047",
        "ProvNo": "0888",
        "ParNonparCode": "K",
        "ParNonparCode2": "K",
    },
              ...

Visual Studio (C#) "Paste Special / Classes from JSON" creates something like this:

public class Lines
{
    public _0010 _0010 { get; set; }
    public _0020 _0020 { get; set; }
}

public class _0010
{
    public string ProvCountyCode { get; set; }
    public string ProvNo { get; set; }
    public string ParNonparCode { get; set; }
    public string ParNonparCode2 { get; set; }
}

public class _0020 
{
    public string ProvCountyCode { get; set; }
    public string ProvNo { get; set; }
    public string ParNonparCode { get; set; }
    public string ParNonparCode2 { get; set; }
}

// ...

This doesn't look very useful to me. Further down, there is a json array called lineSummary that includes a value LineID.

I am thinking I want to reference the dictionary like this:

Lines[LineId]

I can't change the JSON. I am looking to create a class that accomodates it

4

1 Answer 1

0

As said by dbc, you can use a dictionary just like JSON:

public class Lines 
{ 
    public Dictionary<string, Line> lines { get; set; } = new(); 
}

The property names (here 0010 and 0020) will become the keys. This works with both System.Text.Json and Newtonsoft.

New contributor
user32587615 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.