Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Jul 26, 2012 @ 19:46
    Bo Damgaard Mortensen
    0

    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:

    public static string GetNewsNodeById(string id)
            {
                int nodeId = int.Parse(id);
                Node newsNode = new Node(nodeId);
    
                StringBuilder jsonResult = new StringBuilder();
                jsonResult.Append("{\"nodeName\":\"" + newsNode.Name + "\",");
                jsonResult.Append("\"nodeId\":\"" + newsNode.Id + "\",");
    
                jsonResult.Append("\"resume\":\"" + newsNode.GetProperty("resume").Value + "\",");
                jsonResult.Append("\"bodyText\":\"" + newsNode.GetProperty("bodyText").Value + "\",");
                jsonResult.Append("\"images\":\"" + newsNode.GetProperty("billeder").Value + "\",");
                jsonResult.Append("\"publishDate\":\"" + newsNode.GetProperty("publishDate").Value + "\"");
    
                var properties = newsNode.Properties;
                foreach (Property property in properties)
                {
                    jsonResult.Append("{\"alias\":");
                    jsonResult.Append("\"" + property.Alias + "\"");
                    jsonResult.Append(",");
                    jsonResult.Append("\"value\":");
                    jsonResult.Append("\"" + property.Value.Trim() + "\"");
                    jsonResult.Append("},");
                }
    
                jsonResult.Remove(jsonResult.ToString().Length - 1, 1);
    
                jsonResult.Append("}");
    
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                string result = serializer.Serialize(jsonResult.ToString());
                return result;
            }

    But the callback gives me an error in Chromes console:

     Uncaught SyntaxError: Unexpected token L

    When using $.parseJSON(data);

    Has anyone got a clue of how to achieve this? Any help would be greatly appreciated! :-)

    All the best,

    Bo

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Jul 26, 2012 @ 19:47
    Bo Damgaard Mortensen
    0

    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.

  • Douglas Ludlow 210 posts 366 karma points
    Jul 26, 2012 @ 20:34
    Douglas Ludlow
    0

    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):

    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()
    });
    }

    var news = new
    {
    nodeName = newsNode.Name,
    nodeId = newsNode.Id,
    resume = newsNode.GetProperty("resume").Value,
    bodyText = newsNode.GetProperty("bodyText").Value,
    images = newsNode.GetProperty("billeder").Value,
    publishDate = newsNode.GetProperty("publishDate").Value
    properties = properties
    };

    JavaScriptSerializer serializer = new JavaScriptSerializer();
    string json = serializer.Serialize(news);

     

  • Sean Mooney 131 posts 158 karma points c-trib
    Jul 26, 2012 @ 20:35
    Sean Mooney
    0

    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 

     

  • Douglas Ludlow 210 posts 366 karma points
    Jul 26, 2012 @ 20:38
    Douglas Ludlow
    0

    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.

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Jul 26, 2012 @ 21:10
    Bo Damgaard Mortensen
    0

    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 !

Please Sign in or register to post replies

Write your reply to:

Draft