2

I have small problem with prepare table. Generally I usually prepare this table in PHP:

array(2) {
  ["function"]=>
     string(14) "saveStats"
  ["data"]=>
  array(10) {
     ["id"]=>
         string(28) "6079f20ac3_1344412683016_427"
     ["stat"]=>
         array(2) {
             [461]=>
                 string(572) "1834!:!606113;2636701;2532259;8615557991;"
             [462]=>
                 string(664) "947679;1976657;1457921;1302869;2966923;2361071;6876943;5641369;560761;3469061;5438071;9646643;8575873;339307;9652169;2581441;5158451;6210209;6062971;2815237;3396139;2527643;2502571;7882529;536729;1171073;8825407;351427;1845373;8828471;1099463;6653011;5433293;86027;8258377;6748487;5237059;8006827;3996193;117389;7449523;9847147;3476761;7073981;7491493;1603837;8972801;699401;3354649;8120317;3405823;8545499;2672701;1373363;2397077;3417523;8772983;157999;6294929;6024589;6760669;5923937;4560691;8324143;824477;3575549;1372559;986287;4860203;5774323;1832353;4198973;3639841;9179101;718337;4949353;5745787;1608041;8169241;46549;3272723;7622267;"
    }
    ["userAgent"]=>
        string(67) "Mozilla/5.0 (Windows NT 6.1; rv:14.0) Gecko/20100101 Firefox/14.0.1"
    ["ip"]=>
        string(3) "::1"
    ["referer"]=>
        string(4) "Brak"
    ["limit"]=>
        int(1)
  }
}

Data in ["stat"] can be array 10 or 2 end each of them have "random" number (look at [461][462] etc)

This data I must later put to JSON (I use newtonsoft.Json).

How I can prepare this object in C# ?

Bartosz Kowalczyk
  • 1,479
  • 2
  • 18
  • 34

1 Answers1

1

Try this (.NET 4) (see below), the only issue are the columns 461 and 462, C# does not allow variable names starting with a number (I've renamed them to n461 and n462) but it's easy to compensate for this using newtonsoft.Json see similar question here

var table =  new {
    function = "saveStats",
    data = new  {
        id = "6079f20ac3_1344412683016_427" , 
        stat = new List<dynamic>  {
                          new {  n461 = "1834!:!606113;263..." },
                          new {  n462 = "947679;1976657;14..." }
                        },
        userAgent = "Mozilla/5.0 (Windows NT 6.1; rv:14.0) Gecko/20100101 Firefox/14.0.1",
        ip = "::1",
        referer = "Brak",
        limit = 1
    }
 };

I've used dynamic and anonymous classes but you could declare "real" classes of course depending on your needs. (if you are using .NET 3.5 or earlier this is a must)

Community
  • 1
  • 1
Tommy Grovnes
  • 4,126
  • 2
  • 25
  • 40
  • this is brilant. But I have problem with n461 and n462. You see - that json have to go to webservice. And there they expect number. Ok I understand.Thank you – Bartosz Kowalczyk Aug 09 '12 at 12:39
  • I figured as much, the quick and not so pretty solution is to name the columns PLEASE_REMOVE_ME_462 and PLEASE_REMOVE_ME_463, then do the serialization to a Json string then do a string replace on PLEASE_REMOVE_ME_ before sending to web service :) The somewhat nicer solution is to do a custom serializer as shown here http://stackoverflow.com/q/5404303/1045728 – Tommy Grovnes Aug 09 '12 at 12:54
  • Yeah I did it. Thank you again – Bartosz Kowalczyk Aug 09 '12 at 13:06