[{" name":" RAJGOWTHAMAN R"," branch":" B.Tech - Information Technology",&#34 ;详细信息":[{" semester":" 6","子代码":" 10 IT 611","子名":"面向对象的分析和设计","等级":" C","结果":"传递& #34;},{"学期":" 6","子代码":" 10 IT 612","子名&# 34;:"视觉编程","等级":" C","结果":"传递"}, {"学期":" 6","子代码":" 10 IT 613","子名称":&# 34; Web技术","等级":" E","结果":"通过"},{"学期":" 6","子代码":" 10 IT 614","子名":"密码学和网络安全""级":" E""结果":"通行证"},{"学期&#34 ;:" 6","子代码":" 10 IT 615","子名称":"系统软件", "级":" d& #34;"导致":"通行证"},{"学期":" 6""子码" :" 10 IT 6P1"," subname":" Visual Programming Laboratory"," grade":" A", "导致":"通行证"},{"学期":" 6""子码":&#34 ; 10 IT 6P2","子名称":" CASE工具实验室","等级":" C","结果":"通过"},{"学期":" 6","子代码":" 10 IT 6P3& #34;,"子名称":" Web技术实验室","等级":" S","结果" :" Pass"},{" semester":" 6"," subcode":" 10 IT E13", "子名称":"软件质量管理","等级":" RA","结果":&#34 ;故障"}]}]
如何解析上面的json
答案 0 :(得分:1)
Java中有许多JSON库:
这里是使用json-simple解析JSON字符串的代码片段:
import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.JSONValue;
...
String jsonLine = "[{ ... }]";
// first have to parse JSON into object
Object obj = JSONValue.parse(jsonLine);
JSONArray array = (JSONArray)obj;
JSONObject value = (JSONObject)array.get(0);
// JSONObject extends HashMap so can access properties as any Map
System.out.println("keys=" + value.keySet());
JSONArray details = (JSONArray) value.get("details");
// do something with details object
从您的示例中,这将输出:
keys=[name, details, branch]
json-simple有很多解码JSON的例子:
https://github.com/fangyidong/json-simple/wiki
使用gson代码几乎相同:
JsonElement jelement = new JsonParser().parse(jsonLine);
JsonElement value = jelement.getAsJsonArray().get(0);
JsonArray details = value.getAsJsonObject().get("details").getAsJsonArray();