0

Here i 'm trying to build an array from json string that i fetch from a JSON file.My problem is in when converting the string to array.It's not converting.I looked up my JSON file cros-checked it but it seems to be right.But i'm a beginner so doesn't know if there is something i 'm missing.Because of this my application is crashing. My JSON File is:

var Details=          [
            {
                    "Name" : "backtrack" ,
                    "Place" : "CS Software Lab",
                    "Floor" : "1st Floor",
                    "Building" : "Main College Building"
            }

            {
                    "Name" : "mechanist" ,
                    "Place" : "Mechanical Workshop",
                    "Floor" : "1st Floor",
                    "Building" : "Workshop Building"
            }
      ];

And my JSON parser code is this:

 public class JSONParser {

static InputStream iStream = null;
static JSONArray jarray = null;
static String json = "";

public JSONParser() {
}

public JSONArray getJSONFromUrl(String url) {

    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    try {
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        //Log.d("Midhun", statusLine.toString());
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
                Log.d("Midhun",builder.toString());
            }
        } else {
            Log.e("==>", "Failed to download file");
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // Parse String to JSON object
    try {
        jarray = new JSONArray( builder.toString());
        Log.d("Midhun",jarray.toString());
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON Object
    return jarray;

}


}

WHAT I HAVE TRIED

1.I have print my line variable in console.The result is ok.

2.I have print my builder variable in console.It gave me a strange output:

I have attached here that in 3 pics :

enter image description here

enter image description here

enter image description here

What am i doing wrong here.Please help me.

My logcat:

    01-29 11:38:51.177 2126-3446/? E/JSON Parser: Error parsing data org.json.JSONException: Value var of type java.lang.String cannot be converted to JSONArray
    01-29 11:38:51.177 2126-3446/? W/dalvikvm: threadid=24: thread exiting with uncaught exception (group=0x41873e48)
   01-29 11:38:51.177 2126-3446/? E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
    01-29 11:38:51.177 2126-3446/? E/AndroidRuntime: Process: com.defcomdevs.invento16, PID: 2126
    01-29 11:38:51.177 2126-3446/? E/AndroidRuntime: java.lang.RuntimeException: An error occured while executing doInBackground()
    01-29 11:38:51.177 2126-3446/? E/AndroidRuntime:     at android.os.AsyncTask$3.done(AsyncTask.java:300)
    01-29 11:38:51.177 2126-3446/? E/AndroidRuntime:     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
    01-29 11:38:51.177 2126-3446/? E/AndroidRuntime:     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
   01-29 11:38:51.177 2126-3446/? E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
   01-29 11:38:51.177 2126-3446/? E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    01-29 11:38:51.177 2126-3446/? E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
  01-29 11:38:51.177 2126-3446/? E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
   01-29 11:38:51.177 2126-3446/? E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:841)
   01-29 11:38:51.177 2126-3446/? E/AndroidRuntime:  Caused by: java.lang.NullPointerException
   01-29 11:38:51.177 2126-3446/? E/AndroidRuntime:     at com.defcomdevs.invento16.Assistence$GetDetails.doInBackground(Assistence.java:72)
   01-29 11:38:51.177 2126-3446/? E/AndroidRuntime:     at com.defcomdevs.invento16.Assistence$GetDetails.doInBackground(Assistence.java:62)
    01-29 11:38:51.177 2126-3446/? E/AndroidRuntime:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
  01-29 11:38:51.177 2126-3446/? E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
6
  • why are you not using line variable in jsonArray?? Commented Jan 29, 2016 at 6:04
  • this is whole json response Commented Jan 29, 2016 at 6:04
  • I have tried using line variable too.Same output. Commented Jan 29, 2016 at 6:05
  • post your logcat exception Commented Jan 29, 2016 at 6:06
  • 2
    It seems, that your JSON-array do not have separators (commans) between JSON-objects Commented Jan 29, 2016 at 6:07

3 Answers 3

4

There are two syntax mistakes in your JSON

  1. "," should be used to separate JSON objects
  2. ";" you don't have to use semi colon at the end to mark the end of a JSON file

The valid JSON is

[{
        "Name": "backtrack",
        "Place": "CS Software Lab",
        "Floor": "1st Floor",
        "Building": "Main College Building"
    },

    {
        "Name": "mechanist",
        "Place": "Mechanical Workshop",
        "Floor": "1st Floor",
        "Building": "Workshop Building"
    }
]

I recommend you to use jsonlint.com to verify the syntax of your JSON file.

Sign up to request clarification or add additional context in comments.

1 Comment

Add to that the var was also not needed.Thanks
2

Your JsonArray is wrong

you missed ,

try this

  [
            {
                    "Name" : "backtrack" ,
                    "Place" : "CS Software Lab",
                    "Floor" : "1st Floor",
                    "Building" : "Main College Building"
            }
             ,// missed , between two object
            {
                    "Name" : "mechanist" ,
                    "Place" : "Mechanical Workshop",
                    "Floor" : "1st Floor",
                    "Building" : "Workshop Building"
            }
      ]

Online JSON validator/Editor

Comments

0

your json is not valid

[
            {
                    "Name" : "backtrack" ,
                    "Place" : "CS Software Lab",
                    "Floor" : "1st Floor",
                    "Building" : "Main College Building"
            }
,// not present in your code
            {`enter code here`
                    "Name" : "mechanist" ,
                    "Place" : "Mechanical Workshop",
                    "Floor" : "1st Floor",
                    "Building" : "Workshop Building"
            }
      ]

Comments

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.