Im using the following Json file and JavaCode. I would like to be able parse the JSON file but fail to do so. Im working with JSON for the first time.
[
{
"1": "5.645751953125E-3",
"2": "5.79833984375E-3",
"3": "4.57763671875E-3",
"fp": "t1"
},
{
"1": "0.575408935546875",
"2": "0.3570556640625",
"3": "0.2325439453125",
"fp": "t2"
},
{
"fp": ""
}
]
import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class SCR {
public static void main(String[] args) {
JSONParser parser = new JSONParser();
try
{
Object obj = parser.parse(new FileReader("C:\\Users\\Desktop\\2003log.json"));
JSONObject jsonObject = (JSONObject) obj;
System.out.println(jsonObject);
String name = (String) jsonObject.get("fp");
System.out.println(name);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
The errormessage is:
Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject
I can't resolve this. Greateful for help.
Your JSON Object is an array, so you should cast it to JSONArray not to JSONObject
Your obj is an JSONArray containing multiple JSONObjects so you can not cast it to JSONObject.
You should cast it to JSONArray and then you can pull out the objects one by one
Related
I have json file with this format:
{
"1": {
"path": ["C"],
"Des": ["D"]
},
"2": {
"path": ["A"],
"Des": ["D"]
},
"3": {
"path": ["C"],
"Des": ["B"]
}
}
I want to get values in class objects
and using this code to see result before added it to arraylist
objects
import java.io.FileReader;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class test2 {
public static void main(String[] args) {
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("C:/Users/dell/Desktop/streams.json"));
JSONObject jsonObject = (JSONObject) obj;
JSONObject json0 = (JSONObject) jsonObject.get(0);
System.out.println(json0);
} catch (Exception e) {
e.printStackTrace();
}
}
}
The problem is jsonObject is not array, try to do this:
jsonObject.get("1")
Instead of this:
jsonObject.get(0)
To get all keys of the json object simply do:
jsonObject.keySet()
If you want to put keys to the list:
List<String> keys = new ArrayList<>((Set<String>) jsonObject.keySet());
When I use the parser from org.json.simple.parser.* I get an exception whenever one of the values in JSON contains a space. For example:
{"name":"Adam"}
would parse correctly, but
{"name":"Ad am"}
would cause "unexpected token END OF FILE at position 11" exception
Here is the code that I use to convert a JSON string into a JSONObject.
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(stringJSON);
Try to get through this below example and each value contains space except integer one and giving this example just because of you don't have shared your source code.
JSON File(personal_detail.json):
{
"name":"arif mustafa",
"age":26,
"address":["district is Korba","state is Chhattisgarh","country is India"]
}
Java source to read the JSON file format:
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class JSONExample {
public static void main(String[] args) {
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("src/resources/personal_detail.json"));
JSONObject jsonObject = (JSONObject) obj;
System.out.println(jsonObject + "\n");
String name = (String) jsonObject.get("name");
System.out.println("name : " + name);
long age = (Long) jsonObject.get("age");
System.out.println("age : " + age);
//get Object loop array
JSONArray address = (JSONArray) jsonObject.get("address");
System.out.println("address is : ");
Iterator<String> iterator = address.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
The code below shows:
JavaApplication1.java:34: error: non-static method get(Object) cannot be referenced from a static context
JSONArray cars = (JSONArray) JSONObject.get("cars");
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class JavaApplication1 {
#SuppressWarnings("unchecked")
public static void main(String[] args) {
JSONParser parser = new JSONParser();
try {
JSONArray a = (JSONArray) parser.parse(new FileReader("C:/Users/Glambert/Dropbox/java/New folder/perfection/UPdate/json.txt"));
for (Object o : a)
{
JSONObject person = (JSONObject) o;
String name = (String) person.get("name");
System.out.println(name);
String city = (String) person.get("city");
System.out.println(city);
String job = (String) person.get("job");
System.out.println(job);
JSONArray cars = (JSONArray) JSONObject.get("cars");
for (Object c : cars)
{
System.out.println(c+"");
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Anyone has any idea why this is the case?
(by the way, this code was found online and I edited it to test run, so that I can create a new code to take in a different kind of txt file.)
Project: Code from StackOverflow page How to read json file into java with simple JSON library
Code Author: https://stackoverflow.com/users/1212960/greg-kopff
Check this line
JSONArray cars = (JSONArray) JSONObject.get("cars");
change it with
JSONArray cars = (JSONArray) person.get("cars");
Issue was since you are calling the get method directly on the class.
I am trying to read from a json file and get only the phone numbers back i am using java and using the library org.json.simple and i am getting a error that says
"Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject
at Heatmap.main(Heatmap.java:21)"
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class Heatmap {
public static void main(String[] args) {
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("c:\\clients.json"));
JSONObject jsonObject = (JSONObject) obj;
String phone = (String) jsonObject.get("Phone Number");
System.out.println(phone);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException ex) {
Logger.getLogger(Heatmap.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
According to the exception thrown, i think it could be that clients.json it's an array, so you should have something like this:
Object obj = parser.parse(new FileReader("c:\\clients.json"));
JSONArray jsonArray = (JSONArray) obj;
JSONObject client= (JSONObject)jsonObject.get("0");
String phone = (String) client.get("Phone Number");
System.out.println(phone);
Hope it helps !
I have problem in my java class when I try to read from json file
it;s show me this error :
Note : I installed json-simple and use it succeufuly
Exception in thread "main" java.lang.Error: Unresolved compilation
problem: The method parse(FileReader) is undefined for the type
JSONParser
at com.cd.JSONParser.main(JSONParser.java:18)
this is my code :
package com.cd;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class JSONParser {
public static void main(String[] args) {
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("g:\\testm1.json"));
JSONObject jsonObject = (JSONObject) obj;
String name = (String) jsonObject.get("name");
System.out.println(name);
long age = (Long) jsonObject.get("age");
System.out.println(age);
// loop array
JSONArray msg = (JSONArray) jsonObject.get("messages");
Iterator<String> iterator = msg.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
You named your own class the same way as the json-simple JSONParser class. So the compiler resolves JSONParser to your own class (com.cd.JSONParser) and not to the json-simple JSONParser (org.json.simple.parser.JSONParser).
Rename your class. Or use org.json.simple.parser.JSONParser instead of JSONParser each time you want to refer to this class.
Rename your class to something else than JSONParser. This name collides with org.json.simple.parser.JSONParser, so when you call:
JSONParser parser = new JSONParser();
you're actually instantiating your com.cd.JSONParser instead of org.json.simple.parser.JSONParser.
General rule, don't call your classes the same as some library classes that you use (especially JDK classes), unless you really have to. In such case, you can always use the fully qualified name such as:
org.json.simple.parser.JSONParser parser =
new org.json.simple.parser.JSONParser();