Siavash
Siavash

Reputation: 7853

JSONObject(String) constructor is undefined, even though its in the docs

from the android documentation:
"

 JSONObject(String json)

Creates a new JSONObject with name/value mappings from the JSON string."

however my eclipse is complaining that such constructor doesnt exist when I do this:

InputStream ins = appContext.getResources().getAssets().open("bar-data.json");

byte[] bytes = new byte[ins.available()];
ins.read(bytes);
String string = new String(bytes);
JSONObject jsonobj = JSONObject(string);

error: The method JSONObject(String) is undefined for the type BarPage

(barPage is the name of my activity)

edit: this is the library in importing:

import org.json.JSONObject;

Upvotes: 1

Views: 3468

Answers (2)

rickygrimes
rickygrimes

Reputation: 2716

Fix the last line in your code as mentioned by Joe Minichino above. Additionally, you need to use the org.json.JSONObject jar. If you are using Maven, you can download it from here - http://mvnrepository.com/artifact/org.json/json/20090211. If you are having eclipse organize your imports, it picks up the JSON simple jar by default, namely org.json.simple.JSONObject, in which case passing a String argument to the JSONObject constructor won't work.

Upvotes: 1

Joe Minichino
Joe Minichino

Reputation: 2773

I think you forgot to put the "new" keyword on the last line.

Upvotes: 2

Related Questions