Click here to Skip to main content
15,888,052 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to Read Data from a jsonResult?

My Jsonresout returns data{}

C#
{ total = 9, rows = {System.Collections.Generic.List<<>f__AnonymousType9<int, int?, string, string, int?, string>>} }


a list in rows,

how can I read this using JS?

greate

What I have tried:

when using alert(d.rows.length) , it alert(0)

how can I get the data in the json
Posted
Updated 6-Jul-16 20:15pm
Comments
Karthik_Mahalingam 7-Jul-16 1:21am    
Its not a valid json, you are missing the cast while sending the data from server to Client.
july2008 7-Jul-16 1:55am    
Thanks for your reply, Karthik
$.getjosn("/GetList",function(d){
//I want to export the data which GetList Return
})
GetList will return a jsonresult
return Json(new { total = count, rows = result.ToList() }, JsonRequestBehavior.AllowGet)
Karthik_Mahalingam 7-Jul-16 1:58am    
what is "result" type?

does the total and result count is same ?
july2008 7-Jul-16 2:08am    
var result = from v in db.Admin_Menus
join r in db.Admin_Roles on v.F_Role equals r.F_RoleID
into T_Employee_Role
from dept in T_Employee_Role.DefaultIfEmpty()
select new
{
ID = v.F_MenuID,
Level = v.F_Level,
MenuName = v.F_MenuName,
MenuUrl = v.F_MenuUrl,
Sort = v.F_Sort,
Role = dept.F_RoleName
};
int count=result.Count()

It looks there is a JSON syntax in your code.
A common use of JSON is to read data from a web server, and display the data in a web page, JSON data is written as name/value pairs. Curly brackets indicates objects and square for Array
here is basic syntax
JavaScript
"test":[
    {"fname":"abc", "lname":"xxx"}, 
    {"fname":"pqr", "lname":"yyy"}, 
    {"fname":"lmn", "lname":"zzz"}
]

//to read above JSON we need below javascript
var obj = JSON.parse(text);
document.getElementById("lbl1").innerHTML =
 obj.employees[1].firstName + " " + obj.employees[1].lastName;
 
Share this answer
 
v2
Comments
july2008 7-Jul-16 2:13am    
Thanks anyway
My data format is like this, http://wenzhixin.net.cn/examples/bootstrap_table/data
and what I want to do is read the data in the rows .
just use this
C#
return Json(result.ToList(), JsonRequestBehavior.AllowGet);

JavaScript
alert(d.length);//to read the count 
 
Share this answer
 
Comments
july2008 7-Jul-16 2:19am    
I can't use this
return Json(result.ToList(), JsonRequestBehavior.AllowGet);
because I'm using bootstrap-table to load the data,
the data is like this http://wenzhixin.net.cn/examples/bootstrap_table/data .
and what I want to do is read the data in the rows .
Karthik_Mahalingam 7-Jul-16 2:24am    
use this
$.getJSON(url, function (d) {
var json = { count: d.length, rows: d };
debugger;
});

pass the json variable to the bootstrap table
july2008 7-Jul-16 3:01am    
great thanks

but the length for rows is 0, and the count is the right number, then how to get inner content in rows
thanks for your patience
Karthik_Mahalingam 7-Jul-16 3:03am    
does the method is returning rows?
count and rows will be equal.
july2008 7-Jul-16 3:11am    
count is undefined
Local
d
:
Object
json
:
Object
count
:
undefined
rows
:
Object
rows
:
Array[0]
length
:
0
__proto__
:
Array[0]
total
:
9
__proto__
:
Object
__proto__
:
Object
this
:
Object

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900