I read from a WEB server which returns me data like this:
{'status':{"t":1, "f":1, "p":2, "i":1}}
It seems not a valid JSON format data, told by JSON::XS. How can I parse that?
JSON::XS doesn't seem to have a toggle for accepting single quotes, but JSON does.
use strict;
use warnings;
use JSON -support_by_pp
my $source = q( {'status':{"t":1, "f":1, "p":2, "i":1}} );
my $parsed = JSON->new->allow_singlequote->decode($source);
For more options and details, see the JSON module docs.
You can check the specification for the JSON format here. In your case, the problem is likely the single quotes around status. If you use regular double quotes instead, it should parse:
{"status":{"t":1, "f":1, "p":2, "i":1}}
You can check the validity of your JSON at http://jsonlint.com/
It's valid JavaScript. So if you don't want to write your own parser, pass it to a JavaScript engine for evaluation and conversion to JSON.
Related
I am receiving json string through an API from a website. However, the json format sent to me is not the format i want to work with. I want to change the received json string format to the desirable format.
This is the format i received:
{"symbols_returned":147,"base":"USD","data":{"AED":
3.673010,"AFN":75.392000,"ALL":109.685000}}
This however is the format i desire to have:
{"symbols_returned":147,"base":"USD","data":{"AED":"3.673010","AFN":"75.392000","ALL":"109.685000"}}
The difference between the former and latter is the presence of quotation marks on all the numeric currency values in the second json string. It can be seen that the first json string does not have quotes on the numeric currency values.
My question is how can i programmatically turn the first json string to the second json string format using dart programming language. Any help will be appreciated. Thanks
you need to parse the json data before you can use it.
you need to use the json package from dart:convert do do this
import 'dart:convert';
final yourResult = json.decode(API_RESULT);
print(yourResult) // {"symbols_returned":147,"base":"USD","data":{"AED":"3.673010","AFN":"75.392000","ALL":"109.685000"}}
will parse the json and preserve the data types and make that a Map
I'm trying to pass a URL as a query string so it can be read by another website and then used:
www.example.com/domain?return_url=/another/domain
Gets returned as:
www.example.com/domain?return_url=%2Fanother%2Fdomain
Is there a way this URL can be read and parsed by the other application with the escaped characters?
The only way I can think of is to encode it somehow so it comes out like this:
www.example.com/domain?return_url=L2Fub3RoZXIvZG9tYWlu
then the other application can decode and use?
https://www.base64encode.org/
www.example.com/domain?return_url=%2Fanother%2Fdomain
This is called URL encoding. Not because you put a URL in it, but because it encodes characters that have a special meaning in a URL.
The %2F corresponds to a slash /. You've probably also seen the %20 before, which is a space .
Putting a complete URI into a URL parameter of another URI is totally fine.
http://example.org?url=http%3A%2F%2Fexample.org%2Ffoo%3Fbar%3Dbaz
The application behind the URL you are calling needs to be able to understand URL encoding, but that is a trivial thing. Typical web frameworks and interfaces to the web (like CGI.pm or Plack in Perl) will do that. You should not have to care about it a all.
To URL-encode something in Perl, you have several options.
You could use the URI module to create the whole URI including the URL encoded query.
use URI;
my $u = URI->new("http://example.org");
$u->query_form( return_url => "http://example.org/foo/bar?baz=qrr");
print $u;
__END__
http://example.org?return_url=http%3A%2F%2Fexample.org%2Ffoo%2Fbar%3Fbaz%3Dqrr
This seems like the natural thing to do.
You could also use the URI::Encode module, which gives you a uri_encode function. That's useful if you want to encode strings without building a URI object.
use URI::Encode qw(uri_encode uri_decode);
my $encoded = uri_encode($data);
my $decoded = uri_decode($encoded);
All of this is a normal part of how the web works. There is no need to do Base 64 encoding.
The correct way would be to uri-encode the second hop as you do in your first example. The URI and URI::QueryParam modules make this nice and easy:
To encode a URI, you simply create a URI object on your base url. Then add any query parameters that you want. (NOTE: they will be automatically uri-encoded by URI::QueryParam):
use strict;
use warnings;
use feature qw(say);
use URI;
use URI::QueryParam;
my $u = URI->new('http://www.example.com/domain');
$u->query_param_append('return_url', 'http://yahoo.com');
say $u->as_string;
# http://www.example.com/domain?return_url=http%3A%2F%2Fyahoo.com
To receive this url and then redirect to return_url, you simply create a new URI object then pull off the return_url query parameter with URI::QueryParam. (NOTE: again URI::QueryParam automatically uri-decodes the parameter for you):
my $u = URI->new(
'http://www.example.com/domain?return_url=http%3A%2F%2Fyahoo.com'
);
my $return_url = $u->query_param('return_url');
say $return_url;
# http://yahoo.com
I have a spring service which renders JSP page. Spring controller sets JSON data in a request attribute, which i use in my JSP. Can anyone tell how can i parse this JSON data. I know if my web service response was a plain json , i can parse using jsonPath. But in my case JSON is a part of JSP. So I am kinda stuck.
You mean that your JSON is inside your HTML, probably in a Javascript variable?
And I guess using a regex to retrieve the elements your interested in is not enough and that you really want the JSON string?
If so, you can first use a regex check in order to extract your JSON content from your HTML. Beware that if your JSON string is on several lines, you might have to enable some regex features such as multiline or dotall. See
Then, you can transform your regex output into a JSON tree, using the check transform step. There, you can parse the JSON string with the engine of your choice. Gatling ships with json-smart.
Another solution is to build your own check. The procedure for Gatling 2 has been described in this thread on Gatling's Google Group.
When I try to parse ( http://www.roundmenu.com/webservices/index.cfm?ws=listrst&filter=featured ) webservice in json I encounter issues.
If any one can find out what the problem please let me know.
thanks in advance.
That’s not valid JSON. As usual, it’s useful to validate JSON input via http://jsonlint.com.
That particular JSON input fails to properly escape double quotes (\") inside strings.
Put the response in a json formatter like this one on curiousconcept.com. It yields many errors, does look like it's invalid json.
I have some string values that are retrieved from json data. I need a process of converting this strings to xml format and Post it to a url
If it's inside iPhone SDK you will have to parse the string to convert it to XML, unless you can just post the xml as-is.
Anyway. To parse the XML, you have basically two easy options:
NSXMLParser
libxml2 (my preferred choice, don't know why)
Then you can use NSURLConnection class to post the information to the desired XML. There are a lot of Stackoverflow questions already covering this issue you can search and use.
You can also look at an example here.