0

I'm having the following classes on C#:

public class Record
{
    public Record()
    {
       this.Artists = new List<Artist>();       
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public List<Artist> Artists { get; set; }
}

public class Artist
{
    public Artist()
    {
       this.Songs = new List<Song>();       
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public string Album { get; set; }
    public List<Song> Songs { get; set; }
}

public class Song
{
    public Song()
    {

    }

    public int Id { get; set; }
    public string Name { get; set; }        
}

Then we add some data:

Record record = new Record();
record.Id = 1;
record.Name = "Some music";
record.Description = "Something...";


Artist artist = new Artist();
artist.Id = 1;
artist.Name = "Bob Marley";
artist.Album = "Legend";

Song song = new Song();
song.Id = 1;
song.Name = "No woman no cry";
artist.Songs.Add(song);

song = new Song();
song.Id = 2;
song.Name = "Could you be loved";
artist.Songs.Add(song);
record.Artists.Add(artist);

artist = new Artist();
artist.Id = 2;
artist.Name = "Major Lazer";
artist.Album = "Free the universe";

song = new Song();
song.Id = 2;
song.Name = "Get free";
artist.Songs.Add(song);

song = new Song();
song.Id = 2;
song.Name = "Watch out for this";
artist.Songs.Add(song);
record.Artists.Add(artist);

string jsonVal = JsonConvert.SerializeObject(record);
textBox1.Text = jsonVal;

The last 2 lines will serialize the record type object to JSON using Newtonsoft Json and here is the resulted JSON:

{"Id":1,"Name":"Some music","Description":"Something...","Artists":[{"Id":1,"Name":"Bob Marley","Album":"Legend","Songs":[{"Id":1,"Name":"No woman no cry"},{"Id":2,"Name":"Could you be loved"}]},{"Id":2,"Name":"Major Lazer","Album":"Free the universe","Songs":[{"Id":2,"Name":"Get free"},{"Id":2,"Name":"Watch out for this"}]}]}

Now I need to create this JSON using javascript and POST that json to WEB API endpoint. What I don't know is how to create the object in javascript.

I know there is JSON.stringify(object) that will serialize the object, but how can I create the List ???

I know I can do something like this:

var record = 
{
  Name: "Whatever",
  Description: "Something else",

  //How can I make the List<object>      
};

I'm thinking about array and probably this is the right one ... something like this:

var record = 
{
  Name: "Whatever",
  Description: "Something else",
  Artists: 
  {
     "Name": "Artist Name",
     "Album": "Some Album".
     "Songs":
     {
        "Name": "SongName"
     }
  },      
};

and last:

JSON.stringify(record);

MySQL recommendation, field vs relational table

I've got a MySQL INNODB table containing about 2,000,000 rows with 10 fields (table "cars"). It'll keep increasing progressively at a current rate of about 500,000 rows a year. It's a busy table getting different type of queries on average 2-3 times a second 24/7.

The situation right now is that I need to expand the information to include an INT field ("country_id"). But, this field will for at least 99 % of all rows be default "1".

My question is: Would there be any specific reasons to do either of the following solutions:

  1. Add the INT field to the table and index it ("cars"."country_id")
  2. Add a relational table ("car_countries") which includes the fields "car_id" and "country_id"

I setup these examples in the test environment made a few thousand iterations of querying the tables for data to find this out:

  1. Database/table size will due to the index increase with 19 % (~21 MB)
  2. Queries will take on average 16 % longer (0.37717 secs vs 0.32431 secs for 1,000 queries each)

I've previously tried to keep tables filled with appropriate information for all fields and added relational tables where non-mandatory information was needed for a table but now I've read there's little gain in this as long as there's no need to have arrayed data (which MySQL doesn't handle (and PostgreSQL does)) in the table. In my example a specific car will never be sold to 2 countries so there will never be a need to add more countries to a specific car.

Almost everything is easier with solution 1 and since disk space doesn't really matter. Should I still consider solution 2 anyway? If so, why?

Best regards,

/Thomas

4

2 に答える 2

3

Array.NET コレクションには JavaScript を使用する必要があります。

var record = {
  name: "Whatever",
  description: "Something else",
  artists: [{
     name: "Artist Name",
     album: "Some Album",
     songs: [{
        name: "Some Song"
     }]
  }]
};

Web API は、JSON を大文字と小文字を区別しないものとして扱います。

于 2013-10-14T12:31:15.330 に答える
-1

jQuery REST POST JSON リストにはこのコードを使用する必要があります

var jsonObj = [];
$.each({ name: "John", lang: "JS" }, function (key,value) {
jsonObj.push(
{
  key: value
});
});
于 2013-10-14T12:46:18.717 に答える