无法解析使用java嵌套json文件的键和值
下面是我试图解析的json文件。我想打印所有键和相应的值。无法解析使用java嵌套json文件的键和值
{
"A":{
"name":"Ram",
"gender":"male",
"designation":"engineer"
},
"B":{
"name":"Shyam",
"gender":"male",
"designation":"student"
},
"C":{
"name":"Mohan",
"gender":"male",
"designation":"manager"
}
}
我曾尝试下面的代码:从许多网站
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.simple.parser.JSONParser;
public class FetchJsonNested {
public static void main(String args[]) throws FileNotFoundException {
try {
JSONParser jp=new JSONParser();
Object obj=jp.parse(new FileReader("C:\\Users\\DELL\\Documents\\NetBeansProjects\\WaterNetwork\\web\\kusharray.json"));
JSONObject job=(JSONObject)obj;
Iterator <?> keys = job.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
System.out.println(key);
if (job.get(key) instanceof JSONObject) {
System.out.println(job.get(key));
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
我看过的东西,但没有一个是工作像我want.I想打印的所有键和相应值的方式。
下面不遵循真棒和最优雅的解决方案,但这可以引导你到你需要的东西。
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class Main {
public static void main(String[] args) throws Exception {
String jsonString = loadJSONFile();
JsonElement jsonElement = new JsonParser().parse(jsonString);
JsonObject jsonObject = jsonElement.getAsJsonObject();
print(jsonObject);
}
private static String loadJSONFile() throws FileNotFoundException {
Scanner scanner = new Scanner(new FileReader("path/to/the/json/file.ext"));
StringBuilder stringBuilder = new StringBuilder();
while (scanner.hasNext()) {
stringBuilder.append(scanner.next());
}
scanner.close();
return stringBuilder.toString();
}
private static void print(JsonObject jsonObject) {
Set<Map.Entry<String, JsonElement>> entries = jsonObject.entrySet();
for (Map.Entry<String, JsonElement> entry : entries) {
System.out.println(entry.getKey() + ": " + entry.getValue());
try {
JsonElement jsonElement = new JsonParser().parse(String.valueOf(entry.getValue()));
JsonObject innerJsonObject = jsonElement.getAsJsonObject();
print(innerJsonObject);
} catch (Exception e) {
// is not a JSON
}
}
}
}
输出例如:
A: {"name":"Ram","gender":"male","designation":"engineer"}
name: "Ram"
gender: "male"
designation: "engineer"
B: {"name":"Shyam","gender":"male","designation":"student"}
name: "Shyam"
gender: "male"
designation: "student"
C: {"name":"Mohan","gender":"male","designation":"manager"}
name: "Mohan"
gender: "male"
designation: "manager"
非常感谢。但先生的JSON文件是dynamic.so我怎样才能将JSON文件转换为字符串所有时间更新。 –
为此您可以按照此路径:https://*.com/a/4716521/575643;) –
我将编辑答案,一会儿... –
使用org.json,你在你的例子一样:
String jsonStr = "{\"A\":{\"name\":\"Ram\",\"gender\":\"male\",\"designation\":\"engineer\"},\"B\":{\"name\":\"Shyam\",\"gender\":\"male\",\"designation\":\"student\"},\"C\":{\"name\":\"Mohan\",\"gender\":\"male\",\"designation\":\"manager\"}}";
JSONObject json = new JSONObject(jsonStr);
for (Object key : json.keySet().toArray()){
JSONObject data = json.getJSONObject(key.toString());
System.out.println("json :" + data.toString());
System.out.println("name :" +data.getString("name"));
System.out.println("gender :" +data.getString("gender"));
System.out.println("designation :" +data.getString("designation"));
}
现在你可以代替我的第一行 “字符串jsonStr = ...”与您的文件阅读器。
请加输出例如 – gba
我得到一个异常如下: –
java.lang.ClassCastException:org.json.simple.JSONArray不能转换到org.json.JSONObject –