stleary / JSON-java

A reference implementation of a JSON package in Java.
http://stleary.github.io/JSON-java/index.html
Other
4.53k stars 2.56k forks source link

org.json.JSONException: Expected a ',' or '}' at character 26 #463

Closed diegok551 closed 1 year ago

diegok551 commented 5 years ago

help with the error Response Code : 200

{"search-results":{"opensearch:totalResults":"1","opensearch:startIndex":"0","opensearch:itemsPerPage":"1","opensearch:Query":{"@role": "request", "@searchTerms": "DOI(10.1038/504211a)", "@startPage": "0"},"link": [{"@_fa": "true", "@ref": "self", "@href": "https://api.elsevier.com/content/search/scopus?start=0&count=25&query=DOI%2810.1038%2F504211a%29&view=complete", "@type": "application/json"},{"@_fa": "true", "@ref": "first", "@href": "https://api.elsevier.com/content/search/scopus?start=0&count=25&query=DOI%2810.1038%2F504211a%29&view=complete", "@type": "application/json"}],"entry": [{"@_fa": "true", "link": [{"@_fa": "true", "@ref": "self", "@href": "https://api.elsevier.com/content/abstract/scopus_id/84890346350"},{"@_fa": "true", "@ref": "author-affiliation", "@href": "https://api.elsevier.com/content/abstract/scopus_id/84890346350?field=author,affiliation"},{"@_fa": "true", "@ref": "scopus", "@href": "https://www.scopus.com/inward/record.uri?partnerID=HzOxMe3b&scp=84890346350&origin=inward"},{"@_fa": "true", "@ref": "scopus-citedby", "@href": "https://www.scopus.com/inward/citedby.uri?partnerID=HzOxMe3b&scp=84890346350&origin=inward"}],"prism:url":"https://api.elsevier.com/content/abstract/scopus_id/84890346350","dc:identifier":"SCOPUS_ID:84890346350","eid":"2-s2.0-84890346350","dc:title":"Global gender disparities in science","dc:creator":"Larivière V.","prism:publicationName":"Nature","prism:issn":"00280836","prism:eIssn":"14764687","prism:volume":"504","prism:issueIdentifier":"7479","prism:pageRange":"211-213","prism:coverDate":"2013-12-19","prism:coverDisplayDate":"2013","prism:doi":"10.1038/504211a","citedby-count":"232","affiliation": [{"@_fa": "true", "affiliation-url":"https://api.elsevier.com/content/affiliation/affiliation_id/60009507","afid":"60009507","affilname":"University of Montreal","affiliation-city":"Montreal","affiliation-country":"Canada"},{"@_fa": "true", "affiliation-url":"https://api.elsevier.com/content/affiliation/affiliation_id/60021121","afid":"60021121","affilname":"Indiana University","affiliation-city":"Bloomington","affiliation-country":"United States"},{"@_fa": "true", "affiliation-url":"https://api.elsevier.com/content/affiliation/affiliation_id/60021121","afid":"60021121","affilname":"Indiana University","affiliation-city":"Bloomington","affiliation-country":"United States"}],"pubmed-id":"24350369","prism:aggregationType":"Journal","subtype":"no","subtypeDescription":"Note","author-count":{"@limit": "100", "$" :"5"},"author": [{"@_fa": "true", "@seq": "1", "author-url":"https://api.elsevier.com/content/author/author_id/14028805600","authid":"14028805600","authname":"Larivière V.","surname":"Larivière","given-name":"Vincent","initials":"V.","afid": [{"@_fa": "true", "$" :"60009507"}]},{"@_fa": "true", "@seq": "2", "author-url":"https://api.elsevier.com/content/author/author_id/37065109500","authid":"37065109500","authname":"Ni C.","surname":"Ni","given-name":"Chaoqun","initials":"C.","afid": [{"@_fa": "true", "$" :"60021121"}]},{"@_fa": "true", "@seq": "3", "author-url":"https://api.elsevier.com/content/author/author_id/6602494616","authid":"6602494616","authname":"Gingras Y.","surname":"Gingras","given-name":"Yves","initials":"Y.","afid": [{"@_fa": "true", "$" :"60009507"}]},{"@_fa": "true", "@seq": "4", "author-url":"https://api.elsevier.com/content/author/author_id/55605719900","authid":"55605719900","authname":"Cronin B.","surname":"Cronin","given-name":"Blaise","initials":"B.","afid": [{"@_fa": "true", "$" :"60021121"}]},{"@_fa": "true", "@seq": "5", "author-url":"https://api.elsevier.com/content/author/author_id/24465855600","authid":"24465855600","authname":"Sugimoto C.","surname":"Sugimoto","given-name":"Cassidy R.","initials":"C.R.","afid": [{"@_fa": "true", "$" :"60021121"}]}],"source-id":"21206","fund-no":"undefined","openaccess":"1","openaccessFlag":true}]}} org.json.JSONException: Expected a ',' or '}' at character 26 at org.json.JSONTokener.syntaxError(JSONTokener.java:410) at org.json.JSONObject.(JSONObject.java:222) at org.json.JSONTokener.nextValue(JSONTokener.java:344) at org.json.JSONArray.(JSONArray.java:125) at org.json.JSONTokener.nextValue(JSONTokener.java:348) at org.json.JSONObject.(JSONObject.java:205) at org.json.JSONObject.(JSONObject.java:402) at gr.kremmydas.scopusApi.Search.sendGet(Search.java:78) at gr.kremmydas.scopusApi.Search.main(Search.java:29)

if i write other text run if i write the variable what i need

johnjaylward commented 5 years ago

copy/paste with formating...

JSON:

{
  "search-results":{
    "opensearch:totalResults":"1",
    "opensearch:startIndex":"0",
    "opensearch:itemsPerPage":"1",
    "opensearch:Query":{
      "@ROLE":"request",
      "@searchTerms":"DOI(10.1038/504211a)",
      "@startpage":"0"
    },
    "link":[
      {
        "@_fa":"true",
        "@ref":"self",
        "@href":"https://api.elsevier.com/content/search/scopus?start=0&count=25&query=DOI%2810.1038%2F504211a%29&view=complete",
        "@type":"application/json"
      },
      {
        "@_fa":"true",
        "@ref":"first",
        "@href":"https://api.elsevier.com/content/search/scopus?start=0&count=25&query=DOI%2810.1038%2F504211a%29&view=complete",
        "@type":"application/json"
      }
    ],
    "entry":[
      {
        "@_fa":"true",
        "link":[
          {
            "@_fa":"true",
            "@ref":"self",
            "@href":"https://api.elsevier.com/content/abstract/scopus_id/84890346350"
          },
          {
            "@_fa":"true",
            "@ref":"author-affiliation",
            "@href":"https://api.elsevier.com/content/abstract/scopus_id/84890346350?field=author,affiliation"
          },
          {
            "@_fa":"true",
            "@ref":"scopus",
            "@href":"https://www.scopus.com/inward/record.uri?partnerID=HzOxMe3b&scp=84890346350&origin=inward"
          },
          {
            "@_fa":"true",
            "@ref":"scopus-citedby",
            "@href":"https://www.scopus.com/inward/citedby.uri?partnerID=HzOxMe3b&scp=84890346350&origin=inward"
          }
        ],
        "prism:url":"https://api.elsevier.com/content/abstract/scopus_id/84890346350",
        "dc:identifier":"SCOPUS_ID:84890346350",
        "eid":"2-s2.0-84890346350",
        "dc:title":"Global gender disparities in science",
        "dc:creator":"Larivière V.",
        "prism:publicationName":"Nature",
        "prism:issn":"00280836",
        "prism:eIssn":"14764687",
        "prism:volume":"504",
        "prism:issueIdentifier":"7479",
        "prism:pageRange":"211-213",
        "prism:coverDate":"2013-12-19",
        "prism:coverDisplayDate":"2013",
        "prism:doi":"10.1038/504211a",
        "citedby-count":"232",
        "affiliation":[
          {
            "@_fa":"true",
            "affiliation-url":"https://api.elsevier.com/content/affiliation/affiliation_id/60009507",
            "afid":"60009507",
            "affilname":"University of Montreal",
            "affiliation-city":"Montreal",
            "affiliation-country":"Canada"
          },
          {
            "@_fa":"true",
            "affiliation-url":"https://api.elsevier.com/content/affiliation/affiliation_id/60021121",
            "afid":"60021121",
            "affilname":"Indiana University",
            "affiliation-city":"Bloomington",
            "affiliation-country":"United States"
          },
          {
            "@_fa":"true",
            "affiliation-url":"https://api.elsevier.com/content/affiliation/affiliation_id/60021121",
            "afid":"60021121",
            "affilname":"Indiana University",
            "affiliation-city":"Bloomington",
            "affiliation-country":"United States"
          }
        ],
        "pubmed-id":"24350369",
        "prism:aggregationType":"Journal",
        "subtype":"no",
        "subtypeDescription":"Note",
        "author-count":{
          "@limit":"100",
          "$":"5"
        },
        "author":[
          {
            "@_fa":"true",
            "@seq":"1",
            "author-url":"https://api.elsevier.com/content/author/author_id/14028805600",
            "authid":"14028805600",
            "authname":"Larivière V.",
            "surname":"Larivière",
            "given-name":"Vincent",
            "initials":"V.",
            "afid":[
              {
                "@_fa":"true",
                "$":"60009507"
              }
            ]
          },
          {
            "@_fa":"true",
            "@seq":"2",
            "author-url":"https://api.elsevier.com/content/author/author_id/37065109500",
            "authid":"37065109500",
            "authname":"Ni C.",
            "surname":"Ni",
            "given-name":"Chaoqun",
            "initials":"C.",
            "afid":[
              {
                "@_fa":"true",
                "$":"60021121"
              }
            ]
          },
          {
            "@_fa":"true",
            "@seq":"3",
            "author-url":"https://api.elsevier.com/content/author/author_id/6602494616",
            "authid":"6602494616",
            "authname":"Gingras Y.",
            "surname":"Gingras",
            "given-name":"Yves",
            "initials":"Y.",
            "afid":[
              {
                "@_fa":"true",
                "$":"60009507"
              }
            ]
          },
          {
            "@_fa":"true",
            "@seq":"4",
            "author-url":"https://api.elsevier.com/content/author/author_id/55605719900",
            "authid":"55605719900",
            "authname":"Cronin B.",
            "surname":"Cronin",
            "given-name":"Blaise",
            "initials":"B.",
            "afid":[
              {
                "@_fa":"true",
                "$":"60021121"
              }
            ]
          },
          {
            "@_fa":"true",
            "@seq":"5",
            "author-url":"https://api.elsevier.com/content/author/author_id/24465855600",
            "authid":"24465855600",
            "authname":"Sugimoto C.",
            "surname":"Sugimoto",
            "given-name":"Cassidy R.",
            "initials":"C.R.",
            "afid":[
              {
                "@_fa":"true",
                "$":"60021121"
              }
            ]
          }
        ],
        "source-id":"21206",
        "fund-no":"undefined",
        "openaccess":"1",
        "openaccessFlag":true
      }
    ]
  }
}

Log:

org.json.JSONException: Expected a ',' or '}' at character 26
at org.json.JSONTokener.syntaxError(JSONTokener.java:410)
at org.json.JSONObject.(JSONObject.java:222)
at org.json.JSONTokener.nextValue(JSONTokener.java:344)
at org.json.JSONArray.(JSONArray.java:125)
at org.json.JSONTokener.nextValue(JSONTokener.java:348)
at org.json.JSONObject.(JSONObject.java:205)
at org.json.JSONObject.(JSONObject.java:402)
at gr.kremmydas.scopusApi.Search.sendGet(Search.java:78)
at gr.kremmydas.scopusApi.Search.main(Search.java:29)
strkkk commented 5 years ago

@diegok551 what library version you use? I tried with latest (20180813) and it works ok.

stleary commented 5 years ago

Closed for inactivity.

Vikasgupta09 commented 2 years ago

@strkkk , @stleary I am facing same issue with one json object randomly. I have ensured that json object is not changing but there are other libraries in project that might impact the functionality. I am using version 20180813. Is this version specific issue? Do you recommend updating to another version?

Here are few logs: Handling org.json.JSONException: Expected a ',' or ']' at 116718 [character 116719 line 1] at org.json.JSONTokener.syntaxError(JSONTokener.java:505) at org.json.JSONArray.(JSONArray.java:135) at org.json.JSONTokener.nextValue(JSONTokener.java:434) at org.json.JSONObject.(JSONObject.java:245)

stleary commented 1 year ago

Re-opening due to more occurrences. If anyone has additional JSON examples they can share, please post here. @Vikasgupta09 It is unlikely that the parser functionality has changed in any important way since the 20180813 release.

mame15 commented 1 year ago

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is net.sf.json.JSONException: Expected a ',' or ']' at character 8 of

The above error occurs when data is wrapped in [].

"comments": "[test comments]" ==> error "comments": "test comments]" ==> normal "comments": "[test comments" ==> normal

stleary commented 1 year ago

@mame15 I think we need to see more context, e.g. what method are you calling and what are the parameter values when the error occurs?

mame15 commented 1 year ago

it was my mistake. I was using a different library than this one.

library I used -> https://mvnrepository.com/artifact/net.sf.json-lib/json-lib json-lib-1.0b2-jdk13

In the "json-lib-1.0b2-jdk13" library, an error occurs when data is wrapped in []. case 1. "comments": "[test comments]" ==> error case 2. "comments": "test comments]" ==> normal case 3. "comments": "[test comments" ==> normal

where the error occurs is JSONArray.java

line:373 default: throw x.syntaxError("Expected a ',' or ']'");

private JSONArray(JSONTokener x, String[] excludes, boolean ignoreDefaultExcludes) {
    this();
    if (x.nextClean() != '[') {
        throw x.syntaxError("A JSONArray text must start with '['");
    } else if (x.nextClean() != ']') {
        x.back();

        while(true) {
            if (x.nextClean() == ',') {
                x.back();
                this.put((JSON)JSONNull.getInstance());
            } else {
                x.back();
                Object v = x.nextValue(excludes, ignoreDefaultExcludes);
                if (!JSONUtils.isFunctionHeader(v)) {
                    this.add(v, excludes, ignoreDefaultExcludes);
                } else {
                    String params = JSONUtils.getFunctionParams((String)v);
                    int i = 0;
                    StringBuffer sb = new StringBuffer();

                    do {
                        char ch = x.next();
                        if (ch == 0) {
                            break;
                        }

                        if (ch == '{') {
                            ++i;
                        }

                        if (ch == '}') {
                            --i;
                        }

                        sb.append(ch);
                    } while(i != 0);

                    if (i != 0) {
                        throw x.syntaxError("Unbalanced '{' or '}' on prop: " + v);
                    }

                    String text = sb.toString();
                    text = text.substring(1, text.length() - 1).trim();
                    this.elements.add(new JSONFunction(params != null ? StringUtils.split(params, ",") : null, text));
                }
            }

            switch (x.nextClean()) {
                case ',':
                case ';':
                    if (x.nextClean() == ']') {
                        return;
                    }

                    x.back();
                    break;
                case ']':
                    return;
                default:
                    throw x.syntaxError("Expected a ',' or ']'");
            }
        }
    }
}
stleary commented 1 year ago

@mame15 Thanks for the clarification. I will leave this issue open for another week in case someone can provide an example to recreate the problem.

stleary commented 1 year ago

Closing (again) due to lack of activity.