从文本文件中读取json

时间:2013-07-30 11:57:29

标签: java android json

我试图创建一个从Web服务读取json的应用程序,将其保存到文件中,然后读取该文件并解析json。

保存文件的原因是我要读取几个都具有相同布局的json源。

代码:

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();
    }
}

我似乎得到了错误: org.json.JSONException: Value The of type java.lang.String cannot be converted to JSONObject

编辑: 添加了json文件:

{"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."}]}]}]}]}

我做错了什么?

3 个答案:

答案 0 :(得分:2)

我有类似的问题。记录并检查从JSON获得的内容。如果你的JSON无效,它只能修复你的文件,但如果你的json有效,请尝试更改你的行:(增量并尝试直到substring(5))

final JSONObject object = new JSONObject(sText.substring(1));

答案 1 :(得分:1)

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

答案 2 :(得分:0)

您的sText在json中格式不正确,在解析之前验证它