如何从提取的URL解析JSON?

时间:2015-10-14 05:05:14

标签: java 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 个答案:

答案 0 :(得分: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));
  }
 }
}