0

This is the page source.

<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>

I have used the code from: simplest way to read json from a URL in java

But it shows this error:

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)

How do I resolve the error?

Community
  • 1
  • 1
Colu Ali
  • 15
  • 1
  • 5
  • 1
    please ensure your page URL returning exact JSON by entering URL in browser – Jamsheer Oct 14 '15 at 05:57
  • No, the json is get by view source. – Colu Ali Oct 14 '15 at 06:12
  • http://stackoverflow.com/questions/4308554/simplest-way-to-read-json-from-a-url-in-java ,here code seems it reading a valid json from a URL ,please check – Jamsheer Oct 14 '15 at 08:28
  • 1
    The code assumes you are reading JSON and only JSON, not a bunch of html surrounding it. Web APIs normally respond in this way - for example https://httpbin.org/ip – engineerC Oct 16 '15 at 05:27

1 Answers1

0

Use a regex to parse and get the string inside <script type="application/ld+json"> and </script> then feed it to json parser

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));
  }
 }
}
zawhtut
  • 8,335
  • 5
  • 52
  • 76