In your case you don't need to use the JavaScriptSerializer to serialize your jsonResult since that's what you're already doing. Instead of typing out all of the json, you could create create an anonymous object using the object literal notation or a strongly typed object and add assign all of your values to it, then let the JavaScriptSerializer do the work for you. Here's the anonymous version (untested):
int nodeId = int.Parse(id); Node newsNode = new Node(nodeId);
List<dynamic> properties = new List<dynamic>();
foreach (Property property in newsNode.Properties) { properties.Add(new { alias = property.Alias, value = property.Value.Trim() }); }
You guys rock! Thank you so much :-) It worked perfectly - and what's even better: I learned something new today about anonymous types. Guess I'm a bit "too strongly typed" ;-)
Getting a node/document as JSON
Hi all,
First off: I am aware of the awesome Node 2 JSON package ;-)
I'm trying to get my head around getting a node or document by it's id with all of it's properties as JSON from either /base or a generic handler.
So far, I have managed to output a valid (at least according to JSONLint.com) JSON from /base by simply building a string like this:
But the callback gives me an error in Chromes console:
When using $.parseJSON(data);
Has anyone got a clue of how to achieve this? Any help would be greatly appreciated! :-)
All the best,
Bo
Forgot to mention that I can't use the Node 2 JSON package since this is on a 4.5.2 installation.
Should also say that I have also tried to simply serialize a Node object with the standard JavaObjectSerializer, but without any luck.
Hey Bo,
In your case you don't need to use the JavaScriptSerializer to serialize your jsonResult since that's what you're already doing. Instead of typing out all of the json, you could create create an anonymous object using the object literal notation or a strongly typed object and add assign all of your values to it, then let the JavaScriptSerializer do the work for you. Here's the anonymous version (untested):
I would assume that one of your properties (probably bodyText) has a double quote (") in the value, so the JSON is:
"bodyText": "this is the value of "bodyText"",
when it should be:
"bodyText": "this is the value of \"bodyText\"",
The other option would be to create a New Dictionary(String,String) and populate that with the node values, then feed that to the JavaScriptSerializer
Yeah, if you don't like anonymous or don't want to declare a new class then a Dictionary would be the way to go.
You guys rock! Thank you so much :-) It worked perfectly - and what's even better: I learned something new today about anonymous types. Guess I'm a bit "too strongly typed" ;-)
Thanks again !
is working on a reply...