2015-10-14 40 views
0

这是页面源代码。如何解析获取的URL中的JSON?

<script type="text/javascript" src="/_ui/addons/b2ccheckoutaddon/responsive/common/js/acc.termsandconditions.js"></script> 
<script type="text/javascript" src="/_ui/addons/b2ccheckoutaddon/responsive/common/js/acc.payment.js"></script> 
<script type="text/javascript" src="/_ui/addons/b2ccheckoutaddon/responsive/common/js/acc.hopdebug.js"></script> 
<script type="text/javascript" src="/_ui/addons/multipaymentb2ccheckoutaddon/responsive/common/js/acc.multipayment.silentorderpost.js"></script> 
<script type="text/javascript" src="/_ui/addons/ctabanneraddon/responsive/common/js/jquery.countdown.min.js"></script> 
<script type="text/javascript" src="/_ui/addons/ctabanneraddon/responsive/common/js/ctabanneraddon.js"></script> 
<div id="test_cms_productjsonldsnippetcomponent_id_$1" style="display:inline"><!--Availability--> 
    <script type="application/ld+json"> 
    { 
     "@context": "http://schema.org/", 
     "@type": "Product", 
     "name": "A-E2", 
     "description": "A-E2 AC Adapter Kit", 
     "image": "/medias/?context=bWFzdGVyfGltYWdlc3w4MDk0fGltYWdlL2pwZWd8aW1hZ2VzL2g2MC9oNDkvODc5NzA4NjM4NDE1OC5qcGd8ZjdiN", 
     "sku": "514518", 
     "brand":{ 
     "@type": "Brand", 
     "name": "Canon" 
     }, 
     "aggregateRating":{ 
     "@type": "AggregateRating", 
     "ratingValue": "0", 
     "ratingCount": "0" 
     }, 
     "offers":{ 
     "@type": "Offer", 
     "price": "315.52", 
     "priceCurrency": "USD", 
     "availability": "http://schema.org/InStock" 
     } 
    } 
    </script> 
</div> 

我用从代码: simplest way to read json from a URL in java

但它显示了这个错误:

Exception in thread "main" org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1] 
    at org.json.JSONTokener.syntaxError(JSONTokener.java:433) 
    at org.json.JSONObject.<init>(JSONObject.java:198) 
    at org.json.JSONObject.<init>(JSONObject.java:325) 
    at getjson.getjson.JsonReader.readJsonFromUrl(JsonReader.java:83) 
    at getjson.getjson.JsonReader.main(JsonReader.java:128) 

如何解决这个错误吗?

+1

,请确保您的网址在浏览器中输入URL返回准确的JSON – Jamsheer

+0

没有,JSON是通过查看来源获取。 –

+0

http://stackoverflow.com/questions/4308554/simplest-way-to-read-json-from-a-url-in-java,这里的代码似乎是从URL读取有效的json,请检查 – Jamsheer

回答

0

使用正则表达式来解析和进去 <script type="application/ld+json"></script> 字符串,然后将其提供给JSON解析器

import java.util.regex.Pattern; 
import java.util.regex.Matcher; 

class Main { 
    public static void main(String[] args) { 
    Pattern p = Pattern.compile("json\">(.*)</script>"); 
    String s = "<script type=\"application/ld+json\">{aa:11,bb:22,cc{dd:33,ee:55,ff:66}}</script>"; 
    Matcher m = p.matcher(s) ; 
    while (m.find()) { 
     System.out.println("json string= " + m.group(1)); 
    } 
} 
}