简体   繁体   中英

Trying to deserialize JSON object array where the objects have arrays as properties. Can I map the array elements to specific properties of a class?

I am deserializing a json file into C# objects, in this case MTG cards (json from scryfall.com). Previously I solved the problem with MSSQL; I imported the json data into the database with OPENJSON mapping each element I needed to a column with a proper name, and then created a new json file with properties that matched the class I needed in the application. For properties like arena_id that have only one value, or for arrays that were used as strings (color_identity), it was not an issue, but for arrays such as card_faces (cards that have 2 playable sides) I had to specify which element of the array I wanted to import.

    arena_id int                                 '$.arena_id',
...
    [color_identity]                              nvarchar(max) AS JSON,
...
    name_front varchar(100)                      '$.card_faces[0].name',
    mana_cost_front varchar(30)                  '$.card_faces[0].mana_cost',
    name_back varchar(100)                       '$.card_faces[1].name',
    mana_cost_back varchar(30)                   '$.card_faces[1].mana_cost',

From the SQL tailored file I deserialized every object accordingly, but now I'm trying to do it without SQL and with more direct influence as to what goes where. As mentioned before, for properties that do not need to be deconstructed I know I can just pick the same property name in the class and it gets accepted easily. My problem is with array properties that I should divide into separate properties with different names. Is it possible in C# to deserialize in a similar way? I'm thankful for all kinds of hints and tips as I am relatively new to programming with only one year in a training course.

I am guessing that you are simply able to at least call the JSON. C# does have a good JSON deserialization directive.

// keep the class as close to how it will show up on the JSON 
public class MTGCard
{
    public string name;
    public int manaCost;
    ...
}
// then you should be able to just 
currentCard = JsonSerializer.Deserialize<MTGCard>(jsonString);

The class and the json are close enough you should be able to simply use the Array features pull the cards you need and then just deserialize it on the call based

You just need to create a class that properly represents the expected JSON format, EG:

public class MyClass
{
    public int Arena_Id { get; set; }
    public CardFace[] Card_Faces { get; set; }
}

public class CardFace
{
    public string Name { get; set; }
    public string Mana_Cost { get; set; }
}

Then deserialize your JSON:

string json = ... // your data

MyClass x = new Newtonsoft.Json.JsonSerializer() // or whichever serializer you want
    .DeSerialize<MyClass>(json)

With this deserialized instance of your class, you can then translate it to another class matching the structure you require.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM