PDA

View Full Version : JsonStore root is undefined



mohaaron
4 Jun 2009, 9:33 AM
Can anyone tell me what's wrong with my json result. I am getting this error no matter what I do and don't know where my problem is. I'm using asp.net mvc and linq for data access. Below is the json result and the code I'm using.

JSON Result:
"{\"totalRows\":200,\"rows\":[{\"Code\":0,\"Label\":\"Not Processed\"},{\"Code\":1,\"Label\":\"Wish List
\"},{\"Code\":2,\"Label\":\"Review Soon\"},{\"Code\":3,\"Label\":\"Subsequent Release\"},{\"Code\":4
,\"Label\":\"Scheduled for Release\"},{\"Code\":5,\"Label\":\"Immediate Build/Patch\"},{\"Code\":8,\"Label
\":\"Refused\"},{\"Code\":9,\"Label\":\"Non-Problem\"}]}"

aspx



<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="indexContent" ContentPlaceHolderID="Content" runat="server">
<script type="text/javascript" src="../../Scripts/ext-3.0-rc1.1/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="../../Scripts/ext-3.0-rc1.1/ext-all-debug.js"></script>
<script type="text/javascript">
Ext.onReady(function() {
var jsonStore = new Ext.data.JsonStore({
url: '/Bug/GetBugs',
totalProperty: 'totalRows',
root: 'rows',
id: 'Code',
fields: ['Code', 'Label'],
listeners: {
loadexception: function(store, options, response, e) {
console.log(e);
alert(e);
}
}
});

jsonStore.load();

var grid = new Ext.grid.GridPanel({
store: jsonStore,
columns: [
{ header: 'Code', width: 80, sortable: true, dataIndex: 'Code' },
{ header: 'Label', width: 90, sortable: true, dataIndex: 'Label' }
],
viewConfig: {
forceFit: true
},
renderTo: 'content',
title: 'My First Grid',
width: 500,
autoHeight: true,
frame: true
});

grid.getSelectionModel().selectFirstRow();
});
</script>

<div id="content" />

</asp:Content>
C# Controller



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CNET.AppServices;
using CNET.Data.LingServices;
using Newtonsoft.Json;

namespace FixNotesMvcWeb.Controllers
{
[HandleError]
public class BugController : Controller
{
public JsonResult GetBugs()
{
NotesDataContext data = new NotesDataContext();
var priorities = (from b in data.Priorities
select b);

string jresult = "{" +
"\"totalRows\":200," +
"\"rows\":" + JsonConvert.SerializeObject(priorities) + "}";

return this.Json(jresult);
}
}
}

aconran
4 Jun 2009, 9:40 AM
Looks like your JSON is double encoded.

mohaaron
4 Jun 2009, 10:36 AM
Is it all the back slashes that make it double encoded?

mohaaron
4 Jun 2009, 10:40 AM
If I use the following code in the controller I get cleaner output but now it's missing the root element. Am I supposed to be manually adding the root element into the string?



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using CNET.AppServices;
using CNET.Data.LingServices;
using Newtonsoft.Json;

namespace FixNotesMvcWeb.Controllers
{
[HandleError]
public class BugController : Controller
{
public JsonResult GetBugs()
{
NotesDataContext data = new NotesDataContext();
var priorities = (from b in data.Priorities
select b);

return this.Json(priorities);
}
}
}


JSON Result:

[{"Code":0,"Label":"Not Processed"},{"Code":1,"Label":"Wish List"},{"Code":2,"Label":"Review Soon"},
{"Code":3,"Label":"Subsequent Release"},{"Code":4,"Label":"Scheduled for Release"},{"Code":5,"Label"
:"Immediate Build/Patch"},{"Code":8,"Label":"Refused"},{"Code":9,"Label":"Non-Problem"}]The problem is that the root: 'name' is now missing. How do I get this added in?

evant
4 Jun 2009, 10:52 AM
What does this.Json do?

mohaaron
4 Jun 2009, 11:15 AM
This is from the msdn documentation. This is probably the reason why your saying it's double encoded. The trouble is that I don't know how to get my root: 'value' added into the json result properly.

Controller.Json(data)

The JSON result object that serializes the specified object to JSON.
The data parameter must be serializable. The JavaScriptSerializer (http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx) class is used to serialize the object. The result object that is prepared by this method is written to the response by the MVC framework when the object is executed.

evant
4 Jun 2009, 11:17 AM
I tried using the built in JSON serialization and realised it was crap and switched over to JSON.NET. You'd probably have better luck posting this on a .NET forum.

mohaaron
4 Jun 2009, 11:28 AM
OK, I'll try posting this to a .net forum next. I do have one question though. When creating the json for the Ext JsonStore am I supposed to manually create the root: value in the json or is the JsonStore doing it somehow? If I were doing this by calling a php file which returned json would I have to manually format it so the root is written into the json?

heidtmare
4 Jun 2009, 12:08 PM
it will look for an item in the object with a name equal what you supplied for root,
so the below would work fine.


{"totalRows":2,"rows":[{"Code":0,"Label":"Not Processed"},{"Code":1,"Label":"Wish List
"}]}


just return jresult and see what happens.


public JsonResult GetBugs()
{
NotesDataContext data = new NotesDataContext();
var priorities = (from b in data.Priorities
select b);

string jresult = "{" +
"\"totalRows\":200," +
"\"rows\":" + JsonConvert.SerializeObject(priorities) + "}";

//return this.Json(jresult);
return jresult;

}

mohaaron
4 Jun 2009, 12:21 PM
Thanks for the suggestion.

I can't return jresult as a type of JsonResult. The compiler throws the following error.

Cannot implicitly convert type 'string' to 'System.Web.Mvc.JsonResult'

heidtmare
4 Jun 2009, 1:14 PM
well then, if you cant change your return type
then you have to build an object somehow


Object item = new Object();
item.totalRows = 200;
item.rows = priorities

return JsonConvert.SerializeObject(item);
//or
return this.Json(item);
//which ever works
http://james.newtonking.com/projects/json-net.aspx
Thats the docs for the library your using...

mohaaron
4 Jun 2009, 2:20 PM
Thank you for your help. I did post this problem to a asp.net mvc forum and got and answer saying to use a ContentResult instead of JsonResult. This solved my problem. I did also solve my problem using your fix with the additional storage object but it was a lot more work then just switching out the result objects.

Here is a link to the fix in case anyone else needs it.
http://forums.asp.net/p/1431578/3211750.aspx