有没有什么办法可以JSONP格式转换为JSON? [英] Is there any way to convert JSONP format to JSON?

查看:966
本文介绍了有没有什么办法可以JSONP格式转换为JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析来自服务器的响应,我是新来的这个话题,遗憾的是JSONP格式。我不知道如何解析JSONP格式,当我试图用JSON解析器它返回空值。 任何人都可以请帮我做这个...

I am trying to parse a response from server and i am new to this topic, Unfortunately it is in JSONP format. I don't know how to parse JSONP format, when i tried with JSON Parser it is returning null value. Can anyone please help me in doing this...

在此先感谢。

推荐答案

JSONP 只是JSON包裹在一个JavaScript函数呼叫。举例来说,是这样的:

JSONP is just JSON wrapped in a JavaScript function call. For instance, something like:

callback({"status":"success", "someVar":1});

所以,你有两个选择。如果您使用的是的WebView 您可以定义一个名为回调在JavaScript中,然后只需要调用的eval()的JSONP数据。这将调用回调函数,它传递经过分析的JSON对象(的eval()不解析你)

So you have a couple of options. If you are using a WebView you can define a function called callback in JavaScript and then just call eval() on the JSONP data. This will invoke the callback function, passing it the parsed JSON object (the eval() does the parsing for you).

或者,如果你有JSONP字符串在Java code,最简单的方法可能是把解压出来的JSON字符串,如:

Or, if you have the JSONP string in your Java code, the simplest option is probably to extract out the JSON substring, like:

String json = jsonp.substring(jsonp.indexOf("(") + 1, jsonp.lastIndexOf(")"));

这将剥离回调(); ,让你只用 { 状态:成功,someVar:1} ,这应该然后解析与任何标准JSON解析器

That will strip off callback( and );, leaving you with just {"status":"success", "someVar":1}, which should then parse with any standard JSON parser.

这篇关于有没有什么办法可以JSONP格式转换为JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆