0

Hello I am switching over to JSON implementation with AJAX - and need some help understanding this.

Data: There are two parts in the data, first part is a date in UTC time with milliseconds, and the second part is a value. Please suggest, if I should nest this as an array of values or dataobject.

Can someone please tell how this translates to the JSON world (this is a JSON object right?), and

  1. what C# object would generate this JSON object (Mapping, and formatting to milliseconds in UTC time).
  2. From JSONutils What is the difference between datamember, dataProperty and the None option
  3. To get JSON databack, in the Action, should I return type JSON or ActionResult?
  4. On the HTML/JS side, how would I parse this out from an ActonResult or JSON

    {
        "943721039":4,
        "946706653":7,
        "946706350":6,
        "946728112":1
    }
    
Backs
  • 24,430
  • 5
  • 58
  • 85
aggie
  • 798
  • 2
  • 8
  • 23

1 Answers1

1

3-4. just check a simple example at MSDN: https://msdn.microsoft.com/en-us/library/system.web.mvc.jsonresult(v=vs.118).aspx

such method returns simple json when called from ex. jquery $.ajax.

  1. you can go for dynamic as David suggested, and add dynamically properties (with milisecond names) to it, like ex: Dynamically adding properties to a dynamic object?

However, I would avoid it as much as possible. Why not just create a list of objects like {time : '878499204', value: '2'}, much easier to create and then consume. Otherwise, for your json you basically need to use 'for in' on client's side, which is not the coolest way. I'd do it like:

return Json(new []{ new {time = '878499204', value ='2'}, ... } );

this will generate:

[ { time: '878499204', value ='2'}, .... ]

you can also do customization on the serialization and serialize a dictionary to the format you need or create a JSON by yourself, just building it as a string.

  1. read the manual..
Community
  • 1
  • 1
mikus
  • 3,042
  • 1
  • 30
  • 40