Reputation: 195
if my JSON file is like this:
{"result":[{"sentence": "Chinese government cafes people cafe crackdown", "id": 1, "txtfile": "002.txt"}, {"sentence": "kalf alo ldk alf", "id": 2, "txtfile": "003.txt"}]}
How to read .json file into java and parser the JSON to get the sentence, id, txtfile ? since my json consists both JSONARRAY AND JSONOBJECT.
private static String jsonFile="D:\\MYJSON.json";
public static void main(String[] args){
JSONParser parser = new JSONParser();
try{
Object obj=parser.parse(new FileReader(jsonFile));
JSONObject jsonObject = (JSONObject) obj;
String sentence=(String) jsonObject.get("sentence");
System.out.println(sentence);
} catch (Exception e){
e.printStackTrace();
}
}
}
I have the error as:java.lang.ClassCastException: java.lang.String cannot be cast to org.json.JSONObject at yyym.ttt.main(ttt.java:46)
This is my JSON
"{\"result\":[{\"sentence\": \"said Chinese government cafes people cafe crackdown\", \"id\": 1, \"txtfile\": \"002.txt\"}, {\"sentence\": \"kalf alo ldk alf\", \"id\": 2, \"txtfile\": \"003.txt\"}]}"
I checked my JSON file is valid. however, When I print it out. It start with " , how to solve the problem?
the output is "{\"result\":[{\"sentence\": \"said Chinese government cafes people cafe crackdown\", \"id\": 1, \"txtfile\": \"002.txt\"}, {\"sentence\": \"kalf alo ldk alf\", \"id\": 2, \"txtfile\": \"003.txt\"}]}"
org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
at org.json.JSONTokener.syntaxError(JSONTokener.java:451)
at org.json.JSONObject.<init>(JSONObject.java:195)
at org.json.JSONObject.<init>(JSONObject.java:319)
at yyym.ttt.main(ttt.java:26)
Upvotes: 0
Views: 3806
Reputation: 805
I am using this jars file: jsonsimple.jar
http://www.java2s.com/Code/Jar/j/Downloadjsonsimplejar.htm
private static String jsonFile="D:\\MYJSON.json";
public static void main(String[] args) {
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader(jsonFile));
JSONObject jsonObject = (JSONObject) obj;
System.out.println("KK:"+jsonObject.toString());
JSONArray jarray = (JSONArray) jsonObject.get("result");
for (int i=0;i<jarray.size();i++) {
jsonObject = (JSONObject) jarray.get(i);
String sentence = (String)jsonObject.get("sentence");
System.out.println(sentence);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
{"result":[{"sentence":"Chinese government cafes people cafe crackdown","txtfile":"002.txt","id":1},{"sentence":"kalf alo ldk alf","txtfile":"003.txt","id":2}]}
Chinese government cafes people cafe crackdown
kalf alo ldk alf
Upvotes: 1
Reputation: 901
Result is an array, first you have to get that array, loop over that array and then get the desired json objects.
Library used in the following code is - org.json
Code
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Example {
public static void main(String[] args) {
//String jsonString = "{\"result\":[{\"sentence\": \"Chinese government cafes people cafe crackdown\", \"id\": 1, \"txtfile\": \"002.txt\"}, {\"sentence\": \"kalf alo ldk alf\", \"id\": 2, \"txtfile\": \"003.txt\"}]}";
String jsonString = readJsonFile("filePath");
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray result = jsonObject.getJSONArray("result");
for (int i =0; i < result.length(); i++){
JSONObject j = result.getJSONObject(i);
String s = j.getString("sentence");
int id = j.getInt("id");
String txtFile = j.getString("txtfile");
System.out.println("Sentence is " + s);
System.out.println("Id is " + id);
System.out.println("text file is " + txtFile);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
public static String readJsonFile(String filePath) {
String jsonData = "";
BufferedReader br = null;
try {
String line;
br = new BufferedReader(new FileReader(filePath));
while ((line = br.readLine()) != null) {
jsonData += line + "\n";
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return jsonData;
}
}
Output
Upvotes: 2
Reputation: 4241
Use instanceof
to check whether that data is JsonObject or JsonArray
Sample code given below
public void main() {
String jsonFile = "D:\\MYJSON.json";
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader(jsonFile));
if (obj instanceof JSONObject) {
JSONObject jsonObject = (JSONObject) obj;
String sentence = jsonObject.optString("sentence");
System.out.println(sentence);
} else if (obj instanceof JSONArray) {
JSONArray jsonArray = (JSONArray) obj;
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObj = jsonArray.getJSONObject(i);
String sentence = jsonObj.optString("sentence");
System.out.println(sentence);
}//end of for loop
}//end of else if
} catch (Exception e) {
e.printStackTrace();
}
}
Upvotes: 0
Reputation: 1567
String sentence=null;
JSONArray jsonArray = jsonObject.getJSONArray("result");
if(jsonArray.length()>0){
for(int i=0;i<jsonArray.length();i++){
JSONObject jo=jsonArray.getJSONObject(i);
sentence=jo.getString("sentence");
}
}
Upvotes: 0
Reputation: 84
result is JsonArray, and you can loop over it to get JsonObjects e.g:
Object obj=parser.parse(new FileReader(jsonFile));
JSONObject jsonObject = (JSONObject) obj;
JSONArray values = item.json.optJSONArray("result");
JSONObject json;
String sentence = "";
//loop to get object
for (int i = 0; i < values.length(); i++){
json = values.getJSONObject(i);
//get sentenence
sentence = json.getString("sentence");
}
Upvotes: 0