请在下面找到我从JSON文件中读取并显示它的代码。该文件包含一些规则。我想从文件中读取规则并将其显示在UI中。但我得到的输出如下:
Technology: null
Vulnerability: null
Severity: null
RegEx: null文件Rule_File.json不是null,具有值。但是他们没有被这段代码读懂。知道为什么会这样吗?请告诉我你的建议。提前感谢!
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class JSON_Reader
{
public static void main(String args[])
{
JSONParser parser = new JSONParser();
try
{
String text = new String(Files.readAllBytes(Paths.get("C:\\Users\\arathi.variar\\workspace\\Customizable_Rule_UI\\src\\Rule_File.json")), StandardCharsets.UTF_8);
Object object = parser.parse(text);
//convert Object to JSONObject
JSONObject jsonObject = (JSONObject) object;
//Reading the String
String tech = (String) jsonObject.get("Technology");
String vul = (String) jsonObject.get("Vulnerability");
String sev = (String) jsonObject.get("Severity");
String regex = (String) jsonObject.get("RegEx");
//Printing all the values
System.out.println("Technology: " + tech);
System.out.println("Vulnerability: " + vul);
System.out.println("Severity: " + sev);
System.out.println("RegEx: " + regex);
}
catch(FileNotFoundException fe)
{
fe.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}请在我的Rule_File.json下面找到
{
"Angular2": [
{
"Technology": "Angular 2.0",
"Vulnerability": "angular/timeout-service",
"Severity": 1,
"RegEx": "(?=.*(setTimeout))"
},
{
"Technology": "Angular 2.0",
"Vulnerability": "angular/interval-service",
"Severity": 1,
"RegEx": "(?=.*(setInterval))"
},
{
"Technology": "Angular 2.0",
"Vulnerability": "angular/Deferred",
"Severity": 1,
"RegEx": "(?=.*(\\$q\\.defer|\\$q\\_\\.defer))"
},
{
"Technology": "Angular 2.0",
"Vulnerability": "Cross Site Scripting",
"Severity": 1,
"RegEx": "(?=.*(body.*ng-app.*|\\$sceProvider\\.enabled\\(\\s*false\\)))"
},
{
"Technology": "Angular 2.0",
"Vulnerability": "angular/Angular Element",
"Severity": 1,
"RegEx": "(?=.*(\\$\\('.*'\\)|jQuery\\('.*'\\)))"
},
{
"Technology": "Angular 2.0",
"Vulnerability": "Module Setter",
"Severity": 1,
"RegEx": "(?=.*(var\\s*[a-zA-Z0-9_]+\\s*=\\s*angular.module\\(.*\\)))"
},
{
"Technology": "Angular 2.0",
"Vulnerability": "Sensitive Data",
"Severity": 1,
"RegEx": "(?=.*(store\\.set\\())"
},
{
"Technology": "Angular 2.0",
"Vulnerability": "no-cookiestore",
"Severity": 3,
"RegEx": "(?=.*(\\$cookieStore\\s*\\.))"
},
{
"Technology": "Angular 2.0",
"Vulnerability": "no-directive-replace",
"Severity": 3,
"RegEx": "(?=.*((replace\\s*\\:\\s*true)|(\\.replace\\s*\\=\\s*true)))"
},
{
"Technology": "Angular 2.0",
"Vulnerability": "no-http-callback",
"Severity": 3,
"RegEx": "(?=.*(\\$http\\..*\\.(success|error)))"
},
{
"Technology": "Angular 2.0",
"Vulnerability": "defined/undefined",
"Severity": 3,
"RegEx": "(?=.*((value\\s*(\\!|\\=)\\=\\=\\s*undefined)|(\\!angular\\.is(Defined|Undefined))))"
},
{
"Technology": "Angular 2.0",
"Vulnerability": "json functions",
"Severity": 3,
"RegEx": "(?=.*(JSON\\.))"
},
{
"Technology": "Angular 2.0",
"Vulnerability": "Console Log",
"Severity": 3,
"RegEx": "(?=.*(console\\.))"
},
{
"Technology": "Angular 2.0",
"Vulnerability": "no-angular-mock",
"Severity": 3,
"RegEx": "(?=.*(angular\\.mock))"
}
],
"reactJS": [
{
"Technology": "React JS",
"Vulnerability": "Cross Site Scripting",
"Severity": 1,
"RegEx": "(?=.*(window.\\_\\_PRELOADED\\_STATE\\_\\_\\s*=\\s*\\$\\{JSON.Stringify\\(preloadedState\\)}))"
}
],
"javascript": [
{
"Technology": "JAVA/JAVAScript",
"Vulnerability": "URL Injection",
"Severity": 1,
"RegEx": "(?=.*(Request.QueryString[\"[a-zA-Z]+\"];))"
},
{
"Technology": "JAVA/JAVAScript",
"Vulnerability": "Weak Credentials",
"Severity": 1,
"RegEx": "(?=.*((user(name|id)?|logon(id)?)\\s*=\\s*((\\\"|\\').+(\\\"|\\'))))"
}
]
}发布于 2017-11-22 07:53:47
首先需要在数组中找到JSONObject。您正在尝试查找顶级JSONObject的字段,该字段只包含字段Angular2, reactJS, javascript,因此它返回null,因为它找不到technology,vulnerability...字段。
JSONObject jsonObject1 = (JSONObject) object;
JSONArray fields= (JSONArray) jsonObject1.get("Angular2");
for (Object field: fields) {
JSONObject jsonObject = (JSONObject) field;
//Reading the String
String tech = (String) jsonObject.get("Technology");
String vul = (String) jsonObject.get("Vulnerability");
String sev = (String) jsonObject.get("Severity");
String regex = (String) jsonObject.get("RegEx");
//Printing all the values
System.out.println("Technology: " + tech);
System.out.println("Vulnerability: " + vul);
System.out.println("Severity: " + sev);
System.out.println("RegEx: " + regex);
}发布于 2017-11-22 07:53:47
尝尝这个
JSONObject jsonObject = (JSONObject) object;
try {
JSONArray jsonArray= (JSONArray)jsonObject.get("Angular2");
for (Object object: jsonArray) {
JSONObject angular = (JSONObject) object;
String tech = (String) angular.get("Technology");
String vul = (String) angular.get("Vulnerability");
String sev = (String) angular.get("Severity");
String regex = (String) angular.get("RegEx");
}
} catch (JSONException e) {
e.printStackTrace();
}发布于 2017-11-22 07:58:06
您的预期值在Array中,需要以这种方式访问。
JSONArray array = (JSONArray)jsonObject.get("Angular2");
for(Object obj : array.toArray()){
JSONObject jObj = (JSONObject)obj;
System.out.println(String.format("Technology:%s, Vulnerability:%s, Severity:%s, RegEx:%s", jObj.get("Technology"),jObj.get("Vulnerability"),jObj.get("Severity"),jObj.get("RegEx")));
}另一方面,Jackson可以帮助简化到POJO的对象映射。
https://stackoverflow.com/questions/47428908
复制相似问题