ANYTIME I try to parse a file containing a JSON Array with JSON Simple, I end up getting this error message ... code always seems fine in examples but I still get this EVERYTIME I try something ! . please help !
Exception in thread "main" java.lang.ClassCastException:
class org.json.simple.JSONObject cannot be cast to class java.lang.String (org.json.simple.JSONObject is in unnamed module of loader 'app';
java.lang.String is in module java.base of loader 'bootstrap')
an example of an example from this site that still didn't work
JSONParser jsonParser = new JSONParser();
//Parsing the contents of the JSON file
try (Reader reader = new FileReader("example-member-list.json")) {
JSONObject jsonObject = (JSONObject) jsonParser.parse(new FileReader("example-member-list.json"));
JSONArray jsonArray = (JSONArray) jsonObject.get("members");
//Iterating the contents of the array
Iterator<String> iterator = jsonArray.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The problem is with this line.
Iterator<String> iterator = jsonArray.iterator();
JSONArray iterator returns JsonObject Iterator.
Change the above line to
Iterator<JSONObject> iterator = jsonArray.iterator();
Please note that json-simple is not actively maintained. So if you are learning about JSON parsing, I would suggest to check out Google's GSON library (link below).
https://github.com/google/gson
Trying to modify values of a JSONObject within another JSONbject throws cannot find symbol error for method getJSONObject...
I can't use the getJSONObject method, a workaround for me was:
JSONObject Player = (JSONObject) PlayerTemp.get("Player");
Player.put("Language", "German");
However the following would not work:
JSONObject Player = PlayerTemp.getJSONObject("Player");
Here is my whole code example where I try to modify a value within a JSONObject that's nested within another JSONObject:
JSONParser parser = new JSONParser();
try (Reader reader = new FileReader("../resources/PlayerTemp.json")) {
JSONObject PlayerTemp = (JSONObject) parser.parse(reader);
PlayerTemp.getJSONObject("Player").put("Language", "German");
System.out.println(PlayerTemp.get("Player").toString());
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
error: cannot find symbol
PlayerTemp.getJSONObject("Player").put("Language", "German");
^
There is no method called getJSONObject in JSONObject class, so you have to use get() with explicit type casting. If you want more advance methods like getJSONObject consider using Gson and jackson
JSONObject PlayerTemp = (JSONObject) parser.parse(reader);
JSONObject temp = (JSONObject) PlayerTemp.getJSONObject("Player");
temp.put("Language", "German");
When I'm trying to push a JSONObject over a data stream, and I've built the JSONObject properly, but then I try and publish it to the stream it is null.
JSONObject position = new JSONObject();
try {
position.put("lat", location.getLatitude());
position.put("lng", location.getLongitude());
} catch (JSONException e) {
System.out.println("json not work");
e.printStackTrace();
}
System.out.println(position) //result: {"lat":37,"lng":-122}
pubNub.publish().message(position)//never sent, if other type it works
try pubNub.publish().message(position.toString());
response
deneme2="{\"NUKE_USER_NO\":\"1494\",\"WEB_PIN\":\"metin\",\"CARI_NO\":\"2611\",\"FIRMA_ADI\":\"\",\"CARI_UNVANI\":\"LTDSTI\",\"MOD\":\"Müşteri\",\"RENK\":\"'#fbbb5e'\",\"EMAIL\":\"yok\",\"SAHIS_NO\":\"9\",\"ADISOYADI\":\"Metin\",\"YETKILER\":\"UrunListesi, KampanyaList, YeniUrunler, SepetListe, SiparisTakip, BekleyenSiparisler, TaksitBilgi, TaksitliOdeme, CariEkstre, HavaleEftBildir, BankaHesapNumaralari, PcShirbazi, IadeTalep, IadeSonuclari, GarantiSorgulama, ArizaTakip, UyelikBilgilerim, KullaniciList, SevkAdres, CariHareketler, BorcAlacakDurumu, AlisAnalizi, AlisCirolari, BABS, StandartFormlar, ArizaIadeProseduru, SevkiyatProseduru, Organizasyon, Iletisim, SiparisVerme, GuvenliOdeme3D, MailOrderOranlari, KargoTakip, FiyatMod, AnaSayfa, MusteriTemsilcisiniGorsun, Ihaleler, Puanlarim, UyeIsyeri, IadeDegerlendir, IadeSonucDepo\",\"CARI_TIP_NO\":\"6\",\"AKTIF_SEPET\":\"197\",\"KAR_MARJI\":\"0\",\"ODEME_NO\":\"21\",\"DOVIZ_BIRIMI\":\"USD\",\"NAKLIYE_TIP_NO\":\"11\",\"ROLE_ADI\":\"Bayi\"}";
Java code
public JSONObject stringToJson(String deneme2)
{
JSONObject json= new JSONObject();
try {
json.getString(deneme2);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return json;
}
and then error and exception. It doesn't change string value JSONObject
http://i.stack.imgur.com/roVu5.png
Firstly:
JSONObject json= new JSONObject();
is not taking the actual String which need to be converted.
public JSONObject stringToJson(String deneme2)
{
JSONObject json= new JSONObject(deneme2); //pass a String here
try {
json.getString("key name"); //key for which you need to retrieve data
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return json;
}
Hope it helps.
deneme2 is string value and deneme2 is JSON writing format. and then i sent deneme2 stringToJson function for string to JSON object bu return exceptation in image and null json.
Try this it will help you : Firstly convert above String to json object
JSONObject jsonobject = new JSONObject(deneme2);
try {
String abc = json.getString("NUKE_USER_NO");
Log.i("Log", abc);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I have a response String from an API service like this :
{"id":"login","status":"true"}
and This is the way to parse Response String to get Value from Key "Status"
JSONObject jsonObj = null;
try{
jsonObj = new JSONObject(responseString);
}
catch(JSONException e){
e.printStackTrace();
}
JSONArray innerJsonArray = null;
try {
innerJsonArray = jsonObj.getJSONArray("status");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONObject jsonObject = null;
try {
jsonObject = innerJsonArray.getJSONObject(0);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
System.out.println(jsonObject.getString("status"));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
and I've got error
"org.json.JSONArray cannot be converted to JSONObject"
Anyone can give me suggestion?
JSONObject jsonObj = null;
try{
jsonObj = new JSONObject(responseString);
System.out.println(jsonObj.getString("status"));
}
catch(JSONException e){
e.printStackTrace();
}
You do not need to bother with any other arrays.
In which line you get the exception ? In any way may be you should use
jsonObj =JSONObject.fromObject(responseString);
Kindly try the snippet code below. Try this link for gaining better knowledge about parsing an JSON response.
JSONObject jObj;
try {
jObj = new JSONObject(responseString);
Log.i("id==", jObj.getString("id"));
Log.i("status==", jObj.getString("status"));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}