2

I got error in test in this condition.

while (parser.nextToken() != null) {
    if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&  (parser.getText() == 'periodName' )) {          
        break;
    }
}

error is

System.JSONException: Unexpected end-of-input: expected close marker for ARRAY (from [Source: java.io.StringReader@569b9883; line: 1, column: 118]) at input location [1,485]

can anybody help me.

EDIT :

The JSON is :

{"status":"REQUEST_SUCCEEDED","responseTime":42,"message":[],"Results":{ "series": [{"seriesID":"LAUCN040010000000006","data":[{"year":"2014","period":"M08","periodName":"August","value":"20410","footnotes":[{"code":"P","text":"Preliminary."}]},{"year":"2014","period":"M07","periodName":"July","value":"20750","footnotes":[{}]},{"year":"2014","period":"M06","periodName":"June","value":"20863","footnotes":[{}]},{"year":"2014","period":"M05","periodName":"May","value":"20321","footnotes":[{}]},{"year":"2014","period":"M04","periodName":"April","value":"19919","footnotes":[{}]},{"year":"2014","period":"M03","periodName":"March","value":"20309","footnotes":[{}]},{"year":"2014","period":"M02","periodName":"February","value":"20055","footnotes":[{}]},{"year":"2014","period":"M01","periodName":"January","value":"20499","footnotes":[{}]},{"year":"2013","period":"M13","periodName":"Annual","value":"21130","footnotes":[{}]},{"year":"2013","period":"M12","periodName":"December","value":"20358","footnotes":[{}]},{"year":"2013","period":"M11","periodName":"November","value":"20190","footnotes":[{}]},{"year":"2013","period":"M10","periodName":"October","value":"20614","footnotes":[{}]},{"year":"2013","period":"M09","periodName":"September","value":"20963","footnotes":[{}]},{"year":"2013","period":"M08","periodName":"August","value":"21457","footnotes":[{}]},{"year":"2013","period":"M07","periodName":"July","value":"21611","footnotes":[{}]},{"year":"2013","period":"M06","periodName":"June","value":"21684","footnotes":[{}]},{"year":"2013","period":"M05","periodName":"May","value":"21408","footnotes":[{}]},{"year":"2013","period":"M04","periodName":"April","value":"21177","footnotes":[{}]},{"year":"2013","period":"M03","periodName":"March","value":"21059","footnotes":[{}]},{"year":"2013","period":"M02","periodName":"February","value":"21324","footnotes":[{}]},{"year":"2013","period":"M01","periodName":"January","value":"21715","footnotes":[{}]},{"year":"2012","period":"M13","periodName":"Annual","value":"22061","footnotes":[{}]},{"year":"2012","period":"M12","periodName":"December","value":"21695","footnotes":[{}]},{"year":"2012","period":"M11","periodName":"November","value":"21459","footnotes":[{}]},{"year":"2012","period":"M10","periodName":"October","value":"22063","footnotes":[{}]},{"year":"2012","period":"M09","periodName":"September","value":"22312","footnotes":[{}]},{"year":"2012","period":"M08","periodName":"August","value":"22640","footnotes":[{}]},{"year":"2012","period":"M07","periodName":"July","value":"22924","footnotes":[{}]},{"year":"2012","period":"M06","periodName":"June","value":"22479","footnotes":[{}]},{"year":"2012","period":"M05","periodName":"May","value":"21935","footnotes":[{}]},{"year":"2012","period":"M04","periodName":"April","value":"21624","footnotes":[{}]},{"year":"2012","period":"M03","periodName":"March","value":"21778","footnotes":[{}]},{"year":"2012","period":"M02","periodName":"February","value":"21813","footnotes":[{}]},{"year":"2012","period":"M01","periodName":"January","value":"22014","footnotes":[{}]}]}] }}
7
  • 1
    Can you post the JSON parsed ?
    – SF_user
    Oct 8, 2014 at 12:45
  • Thanks for reply {"status":"REQUEST_SUCCEEDED","responseTime":42,"message":[],"Results":{ "series": [{"seriesID":"LAUCN040010000000006","data":[{"year":"2014","period":"M08","periodName":"August","value":"20410","footnotes":[{"code":"P","text":"Preliminary."}]},{"year":"2014","period":"M07","periodName":"July","value":"20750","footnotes":[{}]},{"year":"2014","period":"M06","periodName":"June","value":"20863","footnotes":[{}]},{"year":"2014","period":"M05","periodName":"May","value":"20321","footnotes":[{}]} my json is something like this. Oct 8, 2014 at 12:59
  • Something like this or exactly like this ? It seems that your parser find a wrong character during parsing.
    – SF_user
    Oct 8, 2014 at 13:05
  • This is exactly same but something like that means i paste just part of json because full json is very long i can't post here... and i think no error in json because my all functionality is working fine. i get all data in salesforce from class but only problem in test class. Oct 8, 2014 at 13:14
  • 2
    Copy and paste you test-case JSON through e.g. json.parser.online.fr to make sure there isn't a problem in it. Clearly at the moment there is.
    – Keith C
    Oct 8, 2014 at 13:25

2 Answers 2

0

So looking at the data you've provided I can create this, which runs fine

String jsonText = '{"status":"REQUEST_SUCCEEDED","responseTime":42,"message":[],"Results":{ "series": [{"seriesID":"LAUCN040010000000006","data":[{"year":"2014","period":"M08","periodName":"August","value":"20410","footnotes":[{"code":"P","text":"Preliminary."}]},{"year":"2014","period":"M07","periodName":"July","value":"20750","footnotes":[{}]},{"year":"2014","period":"M06","periodName":"June","value":"20863","footnotes":[{}]},{"year":"2014","period":"M05","periodName":"May","value":"20321","footnotes":[{}]},{"year":"2014","period":"M04","periodName":"April","value":"19919","footnotes":[{}]},{"year":"2014","period":"M03","periodName":"March","value":"20309","footnotes":[{}]},{"year":"2014","period":"M02","periodName":"February","value":"20055","footnotes":[{}]},{"year":"2014","period":"M01","periodName":"January","value":"20499","footnotes":[{}]},{"year":"2013","period":"M13","periodName":"Annual","value":"21130","footnotes":[{}]},{"year":"2013","period":"M12","periodName":"December","value":"20358","footnotes":[{}]},{"year":"2013","period":"M11","periodName":"November","value":"20190","footnotes":[{}]},{"year":"2013","period":"M10","periodName":"October","value":"20614","footnotes":[{}]},{"year":"2013","period":"M09","periodName":"September","value":"20963","footnotes":[{}]},{"year":"2013","period":"M08","periodName":"August","value":"21457","footnotes":[{}]},{"year":"2013","period":"M07","periodName":"July","value":"21611","footnotes":[{}]},{"year":"2013","period":"M06","periodName":"June","value":"21684","footnotes":[{}]},{"year":"2013","period":"M05","periodName":"May","value":"21408","footnotes":[{}]},{"year":"2013","period":"M04","periodName":"April","value":"21177","footnotes":[{}]},{"year":"2013","period":"M03","periodName":"March","value":"21059","footnotes":[{}]},{"year":"2013","period":"M02","periodName":"February","value":"21324","footnotes":[{}]},{"year":"2013","period":"M01","periodName":"January","value":"21715","footnotes":[{}]},{"year":"2012","period":"M13","periodName":"Annual","value":"22061","footnotes":[{}]},{"year":"2012","period":"M12","periodName":"December","value":"21695","footnotes":[{}]},{"year":"2012","period":"M11","periodName":"November","value":"21459","footnotes":[{}]},{"year":"2012","period":"M10","periodName":"October","value":"22063","footnotes":[{}]},{"year":"2012","period":"M09","periodName":"September","value":"22312","footnotes":[{}]},{"year":"2012","period":"M08","periodName":"August","value":"22640","footnotes":[{}]},{"year":"2012","period":"M07","periodName":"July","value":"22924","footnotes":[{}]},{"year":"2012","period":"M06","periodName":"June","value":"22479","footnotes":[{}]},{"year":"2012","period":"M05","periodName":"May","value":"21935","footnotes":[{}]},{"year":"2012","period":"M04","periodName":"April","value":"21624","footnotes":[{}]},{"year":"2012","period":"M03","periodName":"March","value":"21778","footnotes":[{}]},{"year":"2012","period":"M02","periodName":"February","value":"21813","footnotes":[{}]},{"year":"2012","period":"M01","periodName":"January","value":"22014","footnotes":[{}]}]}] }}';
JSONParser parser = JSON.createParser(jsonText);
while (parser.nextToken() != null) {
    if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&  (parser.getText() == 'periodName' )) {          
        break;
    }
}

How are you getting the data into your parser in the test class? Is it a String assignment? The error refers to 1485 so try checking that you haven't introduced a new line or something like that mid-way through the String.

You can also add a debug of the String before invoking the parser. e.g. System.debug(jsonText) to see if it has been corrupted in a way you don't expect.

1
  • yes thank for help. it working fine in my class but error in test class. but its give me now 63% coverage without any error. with this statement in my main class. if (!Test.isRunningTest()){ } Oct 8, 2014 at 13:57
1

A good step in troubleshooting parsing issues is to try validating your data. There are online resources available to parse JSON data like http://jsonlint.com/ and http://jsonformatter.curiousconcept.com/.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .