Forum

November 2nd, 2014
A A A
Avatar

Lost password?
Advanced Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

The forums are currently locked and only available for read only access
sp_Feed Topic RSS sp_TopicIcon
C# and JSON Object
24/03/2009
17:56
Avatar
nsorochan
Member
Members
Forum Posts: 17
Member Since:
29/08/2008
sp_UserOfflineSmall Offline

I am a newbie and I am trying to create a valid json object, so the default jsonreader will be able to read it. The c# code I have is the following:

var gridData = new ViewDataDictionary();
var gridRows = new ViewDataDictionary[1];
gridRows[0] = new ViewDataDictionary();
gridRows[0]["id"] = 2;
gridRows[0]["cell"] = new object[]
{
2, "2009-03-23", "Test Client", 34.65, 5.34, 39.99,
"These are test notes which can be very long."
};
gridData["page"] = 1;
gridData["total"] = 1;
gridData["records"] = 1;
gridData["rows"] = gridRows;

It spits this out, which is obviously not seem correct as the data is not loaded in the grid:
[{"Key":"page","Value":1},{"Key":"total","Value":1},{"Key":"records","Value":1},{"Key":"rows","Value"
:[[{"Key":"id","Value":2},{"Key":"cell","Value":[2,"2009-03-23","Test Client",34.65,5.34,39.99,"These
are test notes which can be very long."]}]]}]

I am not sure what type of objects to use on the server side to create the json object.

thanx

25/03/2009
03:38
Avatar
csetzkorn
UK
Member
Members
Forum Posts: 11
Member Since:
14/03/2009
sp_UserOfflineSmall Offline

Hi,

I am using entity framework and create JSON within ASP.NET MVC. I am using this prototype code and it works:

 public JsonResult Data(string _search, string sord, string sidx)
{
    var result = new JsonResult();

    if( _search.Equals("true") == true ) // search - to be done
    {

    }
    else
    {
 List<test1> list;

 double count = _entities.test1.Count();
 double number_of_requested_rows = Convert.ToDouble( Request.QueryString["rows"] );
 double requested_page = Convert.ToDouble(Request.QueryString["page"]);
 double total_pages = 0;
 var page = new { page = requested_page.ToString() };

 if (count > 0)
 {
     total_pages = Math.Ceiling(count / number_of_requested_rows);
 }

 double start_index =  (number_of_requested_rows * requested_page) - number_of_requested_rows;

 if( start_index < 0 )
 {
     start_index = 0;
 }

 if (sord.Equals("desc") == true)
 {
     list = _entities.test1.OrderBy(sidx, true).Skip((int)start_index).Take((int)number_of_requested_rows).ToList();
 }
 else
 {
     list = _entities.test1.OrderBy(sidx, false).Skip((int)start_index).Take((int)number_of_requested_rows).ToList();
 }

 var rows = from t in list
    select
    new
    {
        id = t.pk,
        cell = new[]
        {
     t.pk.ToString(),
     t.col1,
     t.col2,
     t.col3
        }
    };

 object userdata = new{TYPE = "",MSG = "hello world"};

 result.Data = new { total = total_pages, page = requested_page, records = count, userdata = userdata, rows };
    }

    return result;
}

Perhaps this helps it maps this simple table

pk int,

col1 char,

col2 char,

col3 char

I am sure it can be improved.

 Christian

Forum Timezone: Europe/Sofia

Most Users Ever Online: 715

Currently Online:
24 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

OlegK: 1255

markw65: 179

kobruleht: 144

phicarre: 132

YamilBracho: 124

Renso: 118

Member Stats:

Guest Posters: 447

Members: 11373

Moderators: 2

Admins: 1

Forum Stats:

Groups: 1

Forums: 8

Topics: 10592

Posts: 31289

Newest Members:

, razia, Prankie, psky, praveen neelam, greg.valainis@pa-tech.com

Moderators: tony: 7721, Rumen[Trirand]: 81

Administrators: admin: 66

Comments are closed.
Privacy Policy   Terms and Conditions   Contact Information