0

I'm experiencing some problem loading a JSONArray on the Android. I've constructed the JSON string with Gson and I've checked this online to see whether it is valid. It validates on various online JSON-syntax checkers.

The JSONArray constructor I want to use seems to throw an error when I feed it with a JSONString.

The full error trace from the JSONArray(String json) constructor which causes a problem looks like this.

12-03 15:14:44.290: WARN/System.err(412): org.json.JSONException: Value {"posisjon":"OK","spenning":"0.23","driftsmerking":"1420 10 04","adresse":"KANEBOG�SEN 4","kompNr":"1420","objektnummer":"56569.0","typebetegnelse":"KO500","longitude":"16.5598512563035","latitude":"68.7786964342854","kommune":"1901 HARSTAD","fabrikat":"NEBB"} of type org.json.JSONObject cannot be converted to JSONArray
12-03 15:14:44.290: WARN/System.err(412):     at org.json.JSON.typeMismatch(JSON.java:107) 
12-03 15:14:44.290: WARN/System.err(412):     at org.json.JSONArray.<init>(JSONArray.java:91)
12-03 15:14:44.290: WARN/System.err(412):     at org.json.JSONArray.<init>(JSONArray.java:103)
12-03 15:14:44.290: WARN/System.err(412):     at net.lovholm.kraftwerk.Kraftwerk.onCreate(Kraftwerk.java:66)
12-03 15:14:44.290: WARN/System.err(412):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-03 15:14:44.290: WARN/System.err(412):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-03 15:14:44.290: WARN/System.err(412):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-03 15:14:44.290: WARN/System.err(412):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-03 15:14:44.290: WARN/System.err(412):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-03 15:14:44.290: WARN/System.err(412):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-03 15:14:44.290: WARN/System.err(412):     at android.os.Looper.loop(Looper.java:130)
12-03 15:14:44.290: WARN/System.err(412):     at android.app.ActivityThread.main(ActivityThread.java:3683)
12-03 15:14:44.290: WARN/System.err(412):     at java.lang.reflect.Method.invokeNative(Native Method)
12-03 15:14:44.290: WARN/System.err(412):     at java.lang.reflect.Method.invoke(Method.java:507)
12-03 15:14:44.290: WARN/System.err(412):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-03 15:14:44.290: WARN/System.err(412):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-03 15:14:44.290: WARN/System.err(412):     at dalvik.system.NativeStart.main(Native Method)

Here is an excerpt of the code where the error begins.

try {
    JSONArray json = new JSONArray(finalString);

    for(int i = 0; i < json.length(); i++){
        JSONObject jsonObj = json.getJSONObject(i);
        try {
        Kabelskap ks = getKabelskapFromJSON(jsonObj);
        skap.put(ks.objektnummer, ks);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
} catch (JSONException e) {
    e.printStackTrace();
}

A System.out.println(finalString) gives me this result in the DDMS. The System.out.println() skips long parts of the text. I would presume that the DDMS LogCat is using a charset not supporting Norwegian special letters, and not writing the whole string to log.

11-30 11:02:27.888: INFO/System.out(4304): {"driftsmerking":"1420 10 04","objektnummer":"56569.0","adresse":"KANEBOG�SEN 4","fabrikat":"NEBB","typebetegnelse":"KO500","spenning":"0.23","posisjon":"OK","kompNr":"1420","kommune":"1901 HARSTAD","latitude":"68.7786964342854","longitude":"16.5598512563035"}{"driftsmerking":"4416 01 04","objektnummer":"246349.0","adresse":"NONS�SEN","fabrikat":"ABB","typebetegnelse":"KO300","spenning":"0.23","posisjon":"OK","kompNr":"4416","kommune":"1911 KV�FJORD","latitude":"68.7367985627796","longitude":"16.2694481350258"}{"driftsmerking":"4080 05 02","objektnummer":"275933.0","adresse":"KV�FJORD PLANTESKOLE VEKSTHUS","fabrikat":"ABB","typebetegnelse":"KO700","spenning":"0.23","posisjon":"OK","kompNr":"4080","kommune":"1911 KV�FJORD","latitude":"68.7712454761326","longitude":"16.1901046355049"}{"driftsmerking":"1383 01 02","objektnummer":"150951.0","adresse":"SKILLEVN. 13","fabrikat":"ABB","typebetegnelse":"","spenning":"0.4","posisjon":"OK","kompNr":"1383","kommune":"1901 HARSTAD","latitude":"68.7781436806564","longitude":"16.5643438120601"}{"driftsmerking":"4085 07 02","objektnummer":"275122.0","adresse":"MATHUSET","fabrikat":"NEBB","typebetegnelse":"70","spenning":"0.23","posisjon":"OK","kompNr":"4085","kommune":"1911 KV�FJORD","latitude":"68.7721426826508","longitude":"16.1785193494225"}{"driftsmerking":"3410 05 12","objektnummer":"35184.0","adresse":"HANS EGEDESGT. 20 (ERIKSGATE)","fabrikat":"NEBB","typebetegnelse":"","spenning":"0.23","posisjon":"OK","kompNr":"3410","kommune":"1901 HARSTAD","latitude":"68.7980203542316","longitude":"16.538182961594"}{"driftsmerking":"0800 06 10","objektnummer":"266928.0","adresse":"GAMNES V/MAGNE HARALD OLSEN","fabrikat":"ABB","typebetegnelse":"KO35","spenning":"0.23","posisjon":"OK","kompNr":"","kommune":"1901 HARSTAD","latitude":"68.8206299498345","longitude":"16.4915959572276"}{"driftsmerking":"2580 02 02","objektnummer":"186379.0","adresse":"RESSAN","fabrikat":"ELMEK","typebetegnelse":"50 CM","spenning":"0.23","posisjon":"OK","kompNr":"2580","kommune":"1913 SK�NLAND","latitude":"68.6729130202412","longitude":"17.127746958547"}{"driftsmerking":"1500 10 06","objektnummer":"193875.0","adresse":"TUVSLETTVN. 9","fabrikat":"ABB","typebetegnelse":"KO500","spenning":"0.23","posisjon":"OK","kompNr":"1500","kommune":"1901 HARSTAD","latitude":"68.74741783798","longitude":"16.5517037722856"}{"driftsmerking":"3282 02 02","objektnummer":"35547.0","adresse":"TEGEB�RVN. 12B","fabrikat":"NEBB","typebetegnelse":"","spenning":"0.23","posisjon":"OK","kompNr":"3282","kommune":"1901 HARSTAD","latitude":"68.7991934883357","longitude":"16.5033927383319"}{"driftsmerking":"1430 05 02","objektnummer":"116375.0","adresse":"�VERLAND 2","fabrikat":"ABB","typebetegnelse":"KO700","spenning":"0.23","posisjon":"OK","kompNr":"1430","kommune":"1901 HARSTAD","latitude":"68.7759663213591","longitude":"16.5611455110691"}{"driftsmerking":"4150 05 06","objektnummer":"275496.0","adresse":" NYVN. 21","fabrikat":"ABB","typebetegnelse":"KO500","spenning":"0.4","posisjon":"OK","kompNr":"4150","kommune":"1911 KV�FJORD","latitude":"68.7741925557284","longitude":"16.1726621696438"}{"driftsmerking":"1486 07 02","objektnummer":"162192.0","adresse":"ROHOLTVN. 7","fabrikat":"ABB","typebetegnelse":"KO 700","spenning":"0.23","posisjon":"OK","kompNr":"1486","kommune":"1901 HARSTAD","latitude":"68.7550368601091","longitude":"16.5679658964195"}{"driftsmerking":"3025 02 06 / 3060 /","objektnummer":"35174.0","adresse":"HANS EGEDESGT. 6 (HVEDINGSGATE)","fabrikat":"NEBB","typebetegnelse":"","spenning":"0.23","posisjon":"OK","kompNr":"3025","kommune":"1901 HARSTAD","latitude":"68.7994885383443","longitude":"16.5414817371666"}{"driftsmerking":"7045 03 06","objektnummer":"251410.0","adresse":"KONGSVIKDALEN","fabrikat":"","typebetegnelse":"","spenning":"0.23","posisjon":"OK","kompNr":"7045","kommune":"1852 TJELDSUND","latitude":"68.5682671357777","longitude":"16.2428412985428"}{"driftsmerking":"3010 03 02","objektnummer":"35143.0","adresse":"6.DIVISJONSGT. 10","fabrikat":"ABB","typebetegnelse":"KO500",

I've already created a question here here, and I've followed up the feedback from the answers, but it seems like another problem. Anyhow, perhaps this questions may fill in some blanks.

--- update --- The JSON string which is read by the system looks like this (but is longer). Should an JSON Array be encapsulated by [] for each element or just in the beginning? Is there som way of reading a JSON-collection into a JSONArray structure.

[{"driftsmerking":"1420 10 04","objektnummer":"56569.0","adresse":"KANEBOGÅSEN 4","fabrikat":"NEBB","typebetegnelse":"KO500","spenning":"0.23","posisjon":"OK","kompNr":"1420","kommune":"1901 HARSTAD","latitude":"68.7786964342854","longitude":"16.5598512563035"},{"driftsmerking":"4416 01 04","objektnummer":"246349.0","adresse":"NONSÅSEN","fabrikat":"ABB","typebetegnelse":"KO300","spenning":"0.23","posisjon":"OK","kompNr":"4416","kommune":"1911 KVÆFJORD","latitude":"68.7367985627796","longitude":"16.2694481350258"},{"driftsmerking":"4080 05 02","objektnummer":"275933.0","adresse":"KVÆFJORD PLANTESKOLE VEKSTHUS","fabrikat":"ABB","typebetegnelse":"KO700","spenning":"0.23","posisjon":"OK","kompNr":"4080","kommune":"1911 KVÆFJORD","latitude":"68.7712454761326","longitude":"16.1901046355049"},{"driftsmerking":"1383 01 02","objektnummer":"150951.0","adresse":"SKILLEVN. 13","fabrikat":"ABB","typebetegnelse":"","spenning":"0.4","posisjon":"OK","kompNr":"1383","kommune":"1901 HARSTAD","latitude":"68.7781436806564","longitude":"16.5643438120601"},{"driftsmerking":"4085 07 02","objektnummer":"275122.0","adresse":"MATHUSET","fabrikat":"NEBB","typebetegnelse":"70","spenning":"0.23","posisjon":"OK","kompNr":"4085","kommune":"1911 KVÆFJORD","latitude":"68.7721426826508","longitude":"16.1785193494225"},{"driftsmerking":"3410 05 12","objektnummer":"35184.0","adresse":"HANS EGEDESGT. 20 (ERIKSGATE)","fabrikat":"NEBB","typebetegnelse":"","spenning":"0.23","posisjon":"OK","kompNr":"3410","kommune":"1901 HARSTAD","latitude":"68.7980203542316","longitude":"16.538182961594"},{"driftsmerking":"0800 06 10","objektnummer":"266928.0","adresse":"GAMNES V/MAGNE HARALD OLSEN","fabrikat":"ABB","typebetegnelse":"KO35","spenning":"0.23","posisjon":"OK","kompNr":"","kommune":"1901 HARSTAD","latitude":"68.8206299498345","longitude":"16.4915959572276"}]

2 Answers 2

1

Try with this:

try {
    JSONArray json = new JSONArray(finalString);

    for(int i = 0; i < json.length(); i++){
        JSONObject jsonObj = json.getJSONObject(i);
        try {
        Kabelskap ks = getKabelskapFromJSON(jsonObj);
        skap.put(ks.objektnummer, ks);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
} catch (JSONException e) {
        try {
            JSONObject jsonObj = new JSONObject(finalString);
            Kabelskap ks = getKabelskapFromJSON(jsonObj);
            skap.put(ks.objektnummer, ks);
        } catch (JSONException e1) {
            e1.printStackTrace();
        }
}

This isn't the cleanest way, but it's works :)


Edit

A better way:

if(finalString!=null && !finalString.isEmpty()) {
    if(finalString..startsWith("["))
        try {
            JSONArray json = new JSONArray(finalString);

            for(int i = 0; i < json.length(); i++){
                JSONObject jsonObj = json.getJSONObject(i);
                try {
                    Kabelskap ks = getKabelskapFromJSON(jsonObj);
                    skap.put(ks.objektnummer, ks);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }else{
        try {
            JSONObject jsonObj = new JSONObject(finalString);
            Kabelskap ks = getKabelskapFromJSON(jsonObj);
            skap.put(ks.objektnummer, ks);
        } catch (JSONException e1) {
            e1.printStackTrace();
        }
    }
}
0

The string in your log isn't an array (arrays in JSON are enclosed with square brackets). It's just an object.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.