简体   繁体   English

从文本文件读取json

[英]Read json from text file

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. 我试图制作一个从Web服务读取json的应用程序,将其保存到文件中,然后读取该文件并解析json。

The reason for saving the file is that im going to read several json sources which all have the same layout. 保存文件的原因是我将读取多个具有相同布局的json源。

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 我似乎收到错误: org.json.JSONException: Value The of type java.lang.String cannot be converted to JSONObject

Edit : Added json file : 编辑:添加了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."}]}]}]}]}

What im i doing wrong ? 我在做什么错?

I had a similar problem. 我有一个类似的问题。 Log and check what you get from the JSON. 记录并检查您从JSON获得的内容。 If your JSON is not valid, it won't work just fix your file but if your json is valid, try changing your line like this: (increment and try till substring(5)) 如果您的JSON无效,那么仅修复文件即可,但是如果您的JSON有效,请尝试按以下方式更改行:(递增并尝试直到substring(5))

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

Perhaps Your sText variable is appending some character which is making you JSON string invalid. 也许您的sText变量附加了一些字符,这使您的JSON字符串无效。 print sText and verify it on http://jsonformatter.curiousconcept.com . 打印sText并在http://jsonformatter.curiousconcept.com上进行验证。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM