2017-11-22 9 views
0

JSONファイルから読み込んで表示するコードを以下に示します。ファイルにはいくつかのルールが含まれています。私は、ファイルからルールを読んでUIに表示したいと思います。しかし、出力は次のようになります。JSONオブジェクトの値がnull

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(); 
     } 
    } 
} 

あなたが最初の配列でJSONObjectを見つける必要があり、私の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*((\\\"|\\').+(\\\"|\\'))))" 
      } 
] 
} 
+2

はあなた ' –

+0

ここRule_File.json'は「値が」ポスト?それにはいくつの値がありますか?あなたは配列ではなくオブジェクトであると確信していますか? – shmosel

+0

Rule File.jsonを追加する質問を編集しました。ありがとう! –

答えて

0

を見つけてください。あなたはそれがこの

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(); 
    } 
+0

ありがとう!それは働いています:) –

+0

@ArathiVariar喜んで私は助けました:) –

0

をフィールドを見つけることができないのでAngular2, reactJS, javascriptので、それがnullを返す場合にのみフィールドが含まれているトップレベルJSONObjectのフィールドを見つけようとしているあなた意図した値は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"))); 
} 

、ジャクソンはPOJOのにオブジェクトマッピングを簡素化に役立つ可能性があります。

+0

私は2つのエラーが発生しています:1)メソッドgetJSONArray(文字列)はJSONObject型のために未定義です2)メソッドlength()はJSONArray型のために定義されていません –

+0

何エラーは、ここに投稿 –

+0

@ArathiVariar 1.このように変更JSONArray jsonArray =(JSONArray)jsonObject.get( "Angular2"); –

0

を試してみてください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); 
} 
0

代わりにJSONObjectとJSONArrayを試すことができます。ここで、私はこの問題にアプローチする方法をであることが役に立てば幸い:)

import java.io.FileNotFoundException; 
import java.nio.charset.StandardCharsets; 
import java.nio.file.Files; 
import java.nio.file.Paths; 
import java.util.Iterator; 

// Use these instead as an alternative: 
import org.json.JSONArray; 
import org.json.JSONObject; 

//import org.json.simple.parser.JSONParser; 



public class JSON_Reader 
{ 


public static void readJSONArray() { 
    try 
    { 

     String text = new String(Files.readAllBytes(Paths.get("Rule_File.json")), StandardCharsets.UTF_8); 

     JSONObject jobj = new JSONObject(text); 
     Iterator keys = jobj.keys(); 

     while (keys.hasNext()) { 
      String key = (String) (keys.next()); 


      JSONArray arr = jobj.getJSONArray(key); 

      for (int i = 0; i < arr.length(); i++) { 
       //convert Object to JSONObject 
       JSONObject jsonObject = arr.getJSONObject(i); 

       //Reading the String 
       String tech = (String) jsonObject.get("Technology"); 
       String vul = (String) jsonObject.get("Vulnerability"); 
       Integer sev = (Integer) 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(); 
    } 
} 


public static void main(String args[]) 
{ 

    readJSONArray(); 

} 
} 
関連する問題