I'm trying to return an html snippet from a service that can only return valid JSON.
I've tried some things like:
This gets me a bunch of character like \n\n\n\n\t\t\t\t
return JSON.stringify({html: $('body').html()});
or
return JSON.stringify($('body').html());
On the receiving end, I'd like to be able to parse that HTML via Cheerio, or jQuery or JSDom so I can then run queries like $(".some_selector") on that data.
What is the proper way of doing this? Any special libraries / methods that can handle the escaping for me? I've googled it, but haven't had any clear results...
Thanks.
On the receiving end, you need to simply undo the JSON serialization. That's it!
Your HTML will be in its regular format at obj.html, which you can then parse with whatever DOM parser you want.
Well, you are probably going to need to worry about quotes in the HTML (like with attributes) because the could interfere with the quotes that delimit your JSON values.
Here is similar question as well as this web page that explains some of what you need to consider.
Briefly looking at npmsj.org, I didn't see any reputable modules that might help you make HTML JSON compatible, but I think you can probably figure it out fairly easily on your own, given a large enough sample set of HTML. You can always run your JSON through this validator to check it. I suppose you could also simply do a JSON.parse(jsonContainingHtml) on it as well. You'll get an exception if the string is not valid JSON.
Related
I'm trying to figure out why my parsed request(a json from an jquery ajax, cross domain) is looking a bit strange.
GET /sendjsondata?callback=_testcb&{%22amount%22:1800,%22name%22:%22Vasy%20Jon%22,%22cnp%22:232323,%22coborrower%22:true,%22device%22:%22Desktop%22}&_=1415883870387 404 3.346 ms - 1303
I can create a function to retrieve the piece between braces and then applying a new polish to remove "%22", but I think that I'm dooing a mistake somewhere in my code and that's why I don't obtain a clean json object and maybe someone can tell me where is the issue.
Thank you.
URLs use a special encoding to represent special characters. %22 equals ". Whatever framework you are using should take care of decoding this. Otherwise look for urlencode()/urldecode() functions.
Good day! How are HTML parameters expressed in the routes file? I am trying to pass an HTML but I don't know how. All I know are passing integers ((id: Integer)) and some data types. I tried (content: Html)and (content: Html). I also tried javax.swing.text.html.HTML but it says something about QueryStringBindable. Please help me. Thanks a lot.
Remember that all you pass by route's params will be included in the URL so what is the advantage of using HTML in this place ? GET params should use only simple data types like numerical types, booleans and strings - so you can pass some HTML part as a String (preferably url-encoded or even beter with Base64 encoding).
Much better option is sending it via POST, your URLs won't be terrible long - you won't hit any limitation of URL length, also after common serialization it won't break at special HTML chars.
I've been working with ServiceStack for a while now and recently a new need came up that requires receiving of some html templates inside a JSON request body. I'm obviously thinking about escaping this HTML and it seems to work but I would prefer consumers not to bother escaping a potentially large template. I know ASP.NET is capable of handling this but I wonder if it can be done with ServiceStack.
The following JSON body is received incomplete at the REST endpoint because of the second double quote ...="test...
{
"template" : "<html><body><div name="test"></div></body></html>"
}
"I would prefer consumers not to bother escaping a potentially large template"
I'm not sure I follow. Why would the consumers know anything about escaping a template? Shouldn't that be transparent to the consumer? Any call to JSON.stringify(sourceString) or sourceString.toJson() will automatically escape embedded double-quotes.
"I know ASP.NET is capable of handling this"
Embedded double-quotes must be escaped in valid json syntax. I don't see how ASP.NET wouldn't have the same problem. Am I missing something?
If you view the source of a Google+ profile page, it appears rather complex. It seems most of the data is kept in a huge JSON-like objects. However, they don't seem to be really JSON, since they don't get recognized when I try to decode them. I am hoping the format is more clear to other people here. How would you go about parsing it? It seems it would fairly trivial, if you know where to start.
Here is a sample profile, for example: http://plus.google.com/104560124403688998123
Here's a PHP API I'm working on. It can download and parse the data for a profile page and people's public relationships.
https://github.com/jmstriegel/php.googleplusapi
The JSON piece is a bit mangled. To generate valid JSON, you basically have to remove the first 5 characters that prevent XSRF attacks and then add in all the nulls that have been removed. Here's the code specific to handling parsing the weird Google Plus JSON responses:
https://github.com/jmstriegel/php.googleplusapi/blob/master/lib/GooglePlus/GoogleUtil.php
Call GoogleUtil::FetchGoogleJSON( $url ) and you'll get back a giant array that you can then pull data from. Using this, it should be trivial to make a proxy service to translate stuff into valid json(p) for you to use in your own apps.
I don't have access to Google+ yet, so I'll just answer the general question - that is, how to parse JSON.
JSON is just JavaScript, so parsing it is as simple as evaluating the script. To do this, use the eval() JavaScript function.
var obj = eval('{"JSON":"goes here"}');
Another option is to leverage a console tool. Popular modern browsers pretty much all have them. I recommend Firebug for Firefox in particular.
Using Firefox, log into Google+, then open the Firebug console. You can use the console's dir() command to create a browseable representation of the data. Ex:
console.dir(eval('{"JSON":"goes here"}'));
Sorry I can't be more specific about how to get a handle on Google+'s JSON in particular; without access to the service, this is about the best I can do blind. Good luck!
Thanks to Jason for the excellent php class which reads a profile page into an array.
I've used this class as a base and then parsed it, based upon Russell Beattie's python code from the original appspot rss feed application.
Code here
A few notes:
I use this to merge G+ and WP feeds, hence writing posts into an intermediate array ($items).
I have a convention of creating a pseudo title in Google Plus posts, by emboldening a line and adding two newlines before writing the post. The function getTitle strips this out as a better formatted title in my website and getSummary produces the rest of the post with duplicating the title.
It's made up of a number of parts, an object describing your picasa images, one describing the fields on your profile, one describing your friends.
Most of the long numbers are the internal IDs of people, posts and photos. For instance, my ID is 105249724614922381234. Other than that, it could be parsed if you needed to.
We're adding JSON output to an existing API that outputs XML, to make MobileHTML integration much easier. However, our developer has queried the use of #attributes appearing in the JSON output.
Our original XML looks like:
<markers>
<marker id="11906" latitude="52.226578"
...
and so the JSON comes out as:
callbackname({"marker":[{"#attributes":{"id":"11906","latitude":"52.226578"
....
Our developer has stated:
"Although '#attributes' is legal JSON, it seems to break dot notation, so I can't call data.#attributes. I can call data['#attributes'], so there's a workaround, but it seems safer just to avoid the #-symbol, unless there's a good reason for it."
The XML->JSON(P) conversion is done using:
$xmlObject = simplexml_load_string ($data);
$data = json_encode ($xmlObject);
I want to make our API as easy-to-integrate as possible, and therefore to use standard stuff where possible. But we're using the native PHP json_encode function, so I'd be surprised if it's doing something non-standard.
Is use of #attributes non-standard? Is this basically just the problem that our API is a bit broken in terms of using <marker id..> rather than <marker><id> ?
The JSON standard only specifies what is and isn't valid; it doesn't set convention. There's nothing inherently wrong with using property names which aren't valid Javascript identifiers.
However, as your developer points out, this does make it slightly more awkward to use the result in JS, as it makes it impossible to use dot notation. On the gripping hand, using attributes for simple content is usually seen as "good XML" and you're using using default, built-in tools to convert from XML to JSON. I'd tend to consider that a good enough reason to leave it as it is.
If it were me, I'd look at how difficult it would be to implement a custom XML -> JSON converter. If it's simple and straightforward, go that route and avoid #attribute (it will also likely make your JSON smaller and simpler). If it's too much hassle, however, missing out on dot notation isn't the end of the world. At worst, var attr = data.marker["#attributes"]; will get around the issue.