C# Json-Deserialize

  • C#

Es gibt 1 Antwort in diesem Thema. Der letzte Beitrag () ist von Morrison.

    C# Json-Deserialize

    Hallo,

    ich benutze json.net um Json-String in C# Objekte zu verwandeln, aber ich weiß nicht wie ich folgenden
    String in ein Object verwandeln kann.

    Würde am liebsten haben, dass die "images" in eine List<string> kommen und die "cars" in eine List<car> - aber wie stelle ich das an?

    C#-Quellcode

    1. class Person
    2. {
    3. public string name { get; set; }
    4. public List<string> images { get; set; }
    5. public List<Car> cars { get; set; }
    6. }


    Quellcode

    1. {
    2. "name":"Max Mustermann",
    3. "images":[
    4. "http://example.com/1.jpg",
    5. "http://example.com/2.jpg"
    6. ],
    7. "cars": [
    8. {
    9. "brand": "BMW",
    10. "color": "blue"
    11. },
    12. {
    13. "brand": "Ford",
    14. "color": "red"
    15. }
    16. ]
    17. }

    Guck dir mal diese Seite an: jsonutils.com/

    Ich hab da sowas rausbekommen:

    VB.NET-Quellcode

    1. Public Shared Property jsonResult As Person;
    2. Public Shared Function _deserializeJSON(ByVal _json As String) As Person
    3. jsonResult = JsonConvert.DeserializeObject(Of Person)(_json);
    4. Return jsonResult;
    5. End Function
    6. public class Car
    7. {
    8. public string brand { get; set; }
    9. public string color { get; set; }
    10. }
    11. public class Person
    12. {
    13. public string name { get; set; }
    14. public IList<string> images { get; set; }
    15. public IList<Car> cars { get; set; }
    16. }