0

Im trying to make an app that reads json from a web service, saves it to a file and then reads the file and parses the json.

The reason for saving the file is that im going to read several json sources which all have the same layout.

Code:

public void lesstatusDescription(String sporingsnummer) {
    InputStream oFile;
    try {
        oFile = openFileInput(sporingsnummer + ".json");
        InputStreamReader oReader = new InputStreamReader(oFile);
        BufferedReader oBuffer = new BufferedReader(oReader);
        String sText = "";
        String sEachLine = "";
        while ((sEachLine = oBuffer.readLine()) != null) {
            sText += sEachLine;
        }
        oReader.close();
        final JSONObject object = new JSONObject(sText);
        JSONArray consignmentSet = object.getJSONArray("consignmentSet");
        Integer i = consignmentSet.length();
        TextView tv = (TextView)findViewById(R.id.textView);
        tv.setText(i.toString());

    } catch (Exception ex) {
        //handle exception here
        Toast.makeText(getApplicationContext(),ex.toString(),Toast.LENGTH_SHORT).show();
    }
}

I seem to get the error: org.json.JSONException: Value The of type java.lang.String cannot be converted to JSONObject

Edit : Added json file :

{"consignmentSet":[{"consignmentId":"70300490908546098","previousConsignmentId":"","totalWeightInKgs":0.0,"totalVolumeInDm3":1.2,"packageSet":[{"statusDescription":"","descriptions":[],"packageNumber":"RA608072968SG","previousPackageNumber":"","productName":"REKOMMANDERT BREV FRA UTLANDET","productCode":"2567","brand":"POSTEN","lengthInCm":32,"widthInCm":12,"heightInCm":3,"volumeInDm3":1.2,"weightInKgs":0.0,"dateOfReturn":"03.07.2013","recipientAddress":{"addressLine1":"","addressLine2":"","postalCode":"1458","city":"FJELLSTRAND","countryCode":"NO","country":"Norway"},"eventSet":[{"description":"Sendingen er utlevert","status":"DELIVERED","recipientSignature":{"name":""},"unitId":"171108","unitType":"BRING","postalCode":"1450","city":"NESODDTANGEN","countryCode":"NO","country":"Norway","dateIso":"2013-06-20T15:08:48+02:00","displayDate":"20.06.2013","displayTime":"15:08","consignmentEvent":false},{"description":"Sendingen er ankommet <a title=\"Her finner du Nesoddtangen postkontor\" href=\"https://www.posten.no/kundeservice/postkontor-info-iframe?enhetsnr=171108\" class=\"sporing-lightbox-postoffice\">Nesoddtangen postkontor</a>","status":"READY_FOR_PICKUP","recipientSignature":{"name":""},"unitId":"171108","unitInformationUrl":"http://fraktguide.bring.no/fraktguide/api/pickuppoint/id/171108","unitType":"BRING","postalCode":"1451","city":"NESODDTANGEN","countryCode":"NO","country":"Norway","dateIso":"2013-06-19T08:35:21+02:00","displayDate":"19.06.2013","displayTime":"08:35","consignmentEvent":false},{"description":"Sendingen er ankommet terminal og blir videresendt","status":"IN_TRANSIT","recipientSignature":{"name":""},"unitId":"032850","unitType":"BRING","postalCode":"0024","city":"OSLO","countryCode":"NO","country":"Norway","dateIso":"2013-06-18T16:37:13+02:00","displayDate":"18.06.2013","displayTime":"16:37","consignmentEvent":false,"definitions":[{"term":"terminal","explanation":"Brev, pakke eller godsterminal som benyttes til sortering  og omlasting av sendinger som er underveis til mottaker."}]},{"description":"Sendingen er tollbehandlet og sendes til mottaker.","status":"IN_TRANSIT","recipientSignature":{"name":""},"unitId":"032874","unitType":"BRING","postalCode":"0024","city":"OSLO","countryCode":"NO","country":"Norway","dateIso":"2013-06-18T13:44:56+02:00","displayDate":"18.06.2013","displayTime":"13:44","consignmentEvent":false,"definitions":[{"term":"mottaker","explanation":"En person eller firma som får en sending tilsendt er mottaker. Kalles også adressat. Det vil si den som avsender adresserer sendingen til."}]},{"description":"Sendingen er antatt tollpliktig og er sendt til tollbehandling","status":"CUSTOMS","recipientSignature":{"name":""},"unitId":"100838","unitType":"BRING","postalCode":"1470","city":"LØRENSKOG","countryCode":"NO","country":"Norway","dateIso":"2013-06-17T12:34:28+02:00","displayDate":"17.06.2013","displayTime":"12:34","consignmentEvent":false,"definitions":[{"term":"tollbehandling","explanation":"Bestiller du varer fra utlandet, for eksempel på utenlandske nettsteder, må du fortolle varene. Vi utfører denne tjenesten for deg. \r\nSkal du sende varer utenfor Norges grenser, må sendingene først tollbehandles. Avhengig av vareverdi og innhold kan det kreves at sendingen eksportdeklareres (fortolles)."},{"term":"antatt tollpliktig","explanation":"En sending som antas å ha tollpliktig innhold. Sendingen går videre til tollkontroll. Det betyr ikke nødvendigvis at mottaker må betale toll eller moms, men sendingen blir kontrollert for å avgjøre dette. "}]},{"description":"Sendingen er ankommet mottakerlandet","status":"INTERNATIONAL","recipientSignature":{"name":""},"unitId":"100838","unitType":"BRING","postalCode":"","city":"OSLO LETTER CENTER","countryCode":"NO","country":"Norway","dateIso":"2013-06-17T11:41:48+02:00","displayDate":"17.06.2013","displayTime":"11:41","consignmentEvent":false},{"description":"Sendingen er sendt fra avsenderland","status":"INTERNATIONAL","recipientSignature":{"name":""},"unitId":"032858","unitType":"BRING","postalCode":"","city":"SINGAPORE SØ5","countryCode":"SG","country":"Singapore","dateIso":"2013-06-14T11:22:00+02:00","displayDate":"14.06.2013","displayTime":"11:22","consignmentEvent":false,"definitions":[{"term":"avsenderland","explanation":"Det landet en sendingen er innlevert og sendt ifra."}]},{"description":"Sendingen er ankommet eksportterminal","status":"INTERNATIONAL","recipientSignature":{"name":""},"unitId":"032858","unitType":"BRING","postalCode":"","city":"SINGAPORE SØ5","countryCode":"SG","country":"Singapore","dateIso":"2013-06-13T21:06:00+02:00","displayDate":"13.06.2013","displayTime":"21:06","consignmentEvent":false},{"description":"Sendingen er sendt fra avsenderland","status":"INTERNATIONAL","recipientSignature":{"name":""},"unitId":"032858","unitType":"BRING","postalCode":"","city":"SINGAPORE SØ5","countryCode":"SG","country":"Singapore","dateIso":"2013-06-13T21:06:00+02:00","displayDate":"13.06.2013","displayTime":"21:06","consignmentEvent":false,"definitions":[{"term":"avsenderland","explanation":"Det landet en sendingen er innlevert  og sendt ifra."}]}]}]}]}

What im i doing wrong ?

4

3 回答 3

2

我有一个类似的问题。记录并检查您从 JSON 中获得的信息。如果您的 JSON 无效,则仅修复您的文件将不起作用,但如果您的 json 有效,请尝试像这样更改您的行:(递增并尝试直到子字符串(5))

final JSONObject object = new JSONObject(sText.substring(1));
于 2013-07-30T12:03:57.323 回答
1

也许您的 sText 变量正在附加一些使您的 JSON 字符串无效的字符。打印 sText 并在http://jsonformatter.curiousconcept.com上进行验证。

于 2013-07-30T12:08:36.987 回答
0

您的 sText 在 json 中的格式不正确,请在解析之前对其进行验证

于 2013-07-30T12:04:14.740 回答