-4

I'm quite new to JSON, specially serialization and deserialization. I have JSON like this, which i first serialize and put it inside variable. Now i want to get data out of this variable.

{
    "timestamp": "2017-06-20 12:12:10",
    "categories":
[
    {
      "name": "Fiction",
    },
    {
      "name": "Roman",    
    }
]
,
    "types":
[
    {
      "name": "Long story",
    },
    {
      "name": "Short story",      
    }
],
    "books":
[
    {
        "title": "Song of ice and fire",
        "bookNumber": "1234567",
        "aisle":
        [
            {
                "location": "fiction isle",
            },
            {
                "location": "roman aisle",
            }
        ]
    }
]
}

Stored in my var json

** UPDATE 1**

    public List<Books> listOfBooks()
        {
            var bookList = new List<Books>
            {
                new Books
                {
                    title = "song of ice and fire"
                    bookNumber = "1234567",
                    aisle = listOfAisles()           
                }
            };
            return bookList;
        }

public List<Aisle> listOfAisles
{
        var aisleList = new List<Aisle>
            {
                new Aisle
                {
                    location = "fiction aisle"

                }
            };
            return aisleList;
}

MainProgram registerBooks = new MainProgram();
        var obj = new MainProgram
        {
            Timestamp = "2017-06-20 09:10:55",
            Books = listOfBooks(),
            listOfAisles = listOfAisles(),
            categories = listOfCategories(),
            types = listofTypes()
        };

var _myJson = new JavaScriptSerializer().Serialize(object);

Books _books = JsonConvert.DeserializeObject<Books>(_myJson);

string _time = _books.Timestamp;

When i debug i get timestamp and put it in _time and it works fine...but when i try to get other items like categories.name, i can't reach those items.

The problem is i may have many List items inside this JSON, not just one...i could have like 100 categories. how to get those items back?

How should i do this?

Thank you!

edit

public class Books
{
    public string title {get;set;}
    public string bookNumber {get;set;}
    public List<aisle> aisles {get;set;}
}

PS - lower/uppercase isn't the issue! it is due translation to english

aiden87
  • 929
  • 8
  • 25
  • 52

1 Answers1

1

The categories are list within a list. So, if you want to get them. Find specific category by linq or iterate foreach library and print them all. Depends on needs.

Library library = JsonConvert.DeserializeObject<Library>(_myJson);
//find category by name
Category finddedCategory = library.categories.FirstOrDefault(x => x.name == "sci-fi");
//print out all names of categories
foreach(Category c in library.categories)
{
 Console.Write(c.name);
}
raichiks
  • 286
  • 4
  • 16
  • i think this might be what i'm looking for, but i dont understand the x.name == "sci-fi" part. why is that needed? what would happen if i didnt know any categories name in program? and yes i need to loop through them and print all of them. so if there are 100 categories, i need to print all of them, and ofcourse all the rest (books, types, aisles...etc) – aiden87 Aug 01 '17 at 08:09
  • x.name == "sci-fi" - that is to find specific name in categories. – raichiks Aug 01 '17 at 08:11
  • let me debug that and see what happens – aiden87 Aug 01 '17 at 08:13
  • added a comments to my code. and foreach cycle goes throught all categoies and prints out all names, no need to know the names – raichiks Aug 01 '17 at 08:14
  • instead of console.write(c.name), what would be the best way to store all the variables...since after ill be getting that data ill be inserting it into database? – aiden87 Aug 01 '17 at 08:15
  • a simple string will do, or do i need a list? but since im looping i'm thinking string? – aiden87 Aug 01 '17 at 08:17
  • these categories allready is stored in `library.categoies` after deserialization. console.write(c.name) -is just an example how to access them and print out on console – raichiks Aug 01 '17 at 08:18