The JSONParser class is lower down on the tutorial it looks like this...
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET method
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
I would consider looking at the jackson library tho http://jackson.codehaus.org/
Answer from user1791586 on Stack OverflowIt seems like you are missing a dependency json-simple.
1) Here is the link JSON.simple » 1.1.1 where you can download missing dependency.
2) Add downloaded dependency

3) If you have the maven or gradle project then copy dependencies line.

4) Then clean build the project and see the magic.
Here As you're facing an error like; JSONParser it shows "The constructor JSONParser() is deprecated" because You've not used it correctly to get your JSON file data I've posted below sample code where you can get the idea.
import java.io.FileReader;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class JsonFileReader {
public static void main(String[] args) {
try {
JSONParser parser = new JSONParser();
JSONObject obj = (JSONObject)parser.parse(new FileReader("L:/data.json"));
String name = (String) obj.get("USERNAME");
String password = (String) obj.get("PASSWORD");
System.out.println("Name: " + name);
System.out.println("Password: " + password);
} catch (Exception ex) {
System.out.println("Exception: "+ ex.getMessage());
}
}
}
note: Here I've used the same "json-simple-1.1.1.jar" file.
File: data.json
{
"USERNAME": "abc",
"PASSWORD": "xyz"
}
Videos
For Android Studio, just add this to the build.gradle dependencies:
compile 'com.googlecode.json-simple:json-simple:1.1'
For other IDEs and other options, see here: http://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple/1.1
The other answer is a bit out-dated. ( At the end of 2018 compile will be either api or implementation also there is a new version 1.1.1 )
Add this to the build.gradle dependencies:
implementation group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
and then import
import org.json.simple.parser.JSONParser;
Hope it helps as it does to me :)
use these dependencies
jackson-databind
jackson-annotations
jackson-core
public class JsonTest {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper mapper=new ObjectMapper();
Map<String,String> dt=new Hashtable();
dt.put("1", "welcome");
dt.put("2", "bye");
String jsonString = mapper.writeValueAsString(dt)
System.out.println(jsonString);
}
}
Looks like mixed up references.
You might be using a library that uses an old version of Jackson itself (i.e. the org.codehaus package)...
I usually just reference Jackson through Maven.
Something like:
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>LATEST</version>
</dependency>
</dependencies>