JSON对象值为null

时间:2017-11-22 07:20:12

标签: java json

请在下面找到我用来从JSON文件中读取并显示它的代码。该文件包含一些规则。我想从文件中读取规则并将其显示在UI中。但我得到的输出如下:

    <div id="civinfo">
        <h1>Information</h1>
        <!-- CIV SIDE -->
        <div>
            <h2>Civilian: </h2>
            <span><h3>Name: </h3><p id="civname">None, None</p></span>
            <span><h3>Warrant: </h3><p id="civwarrant">None</p></span>
            <span><h3>Citations: </h3><p id="civcit">None</p></span>
        </div>
        <!-- VEH SIDE -->
        <div>
            <h2>Vehicle: </h2>
            <span><h3>Plate: </h3><p id="vehplate">None</p></span>
            <span><h3>Stolen: </h3><p id="vehstolen">None</p></span>
            <span><h3>Registered: </h3><p id="vehregi">None</p></span>
            <span><h3>Insured: </h3><p id="vehinsured">None</p></span>
        </div>
    </div>

文件Rule_File.json不为null且具有值。但他们没有被这段代码读过。知道为什么会这样吗?请让我知道你的建议。提前谢谢!

#civinfo {
    margin: 0;
    padding: 0;
    position: absolute;
    top: 50%;
    left: 50%;
    background: #000000;
    width: 35%;
    height: 30%;
    display: none;
    font-family: 'Lato', sans-serif;
    color: white;
    border-radius: 10px;
}
#civinfo h1 {
    text-align: center;
    font-size: 35px;
    margin: 8px;
}
#civinfo div {
    margin: 0;
    padding: 0;
    display: inline-block !important;
    width: 45%;
    height: auto !important;
}
#civinfo div h2 {
    text-align: center;
    margin: 0 5px;
    font-weight: 400;
    font-size: 20px;
}
#civinfo div h3 {
    display: inherit;
    font-weight: 400;
    margin: 0 10px;
}
#civinfo div span {
    display: inline-block;
    margin: 0;
    padding: 0;
    margin-bottom: 5px;
    width: 75%;
    height: auto;
}
#civinfo div p {
    float: center;
    text-align: center;
    display: inherit;
    padding: 5px 15px;
    background: #bbbbbb;
    color: black;
    border-radius: 25px;
    font-size: 16px;
}

请在下面找到我的Rule_File.json

Technology: null
Vulnerability: null
Severity: null
RegEx: null

4 个答案:

答案 0 :(得分:0)

您需要先在数组中找到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);
}

答案 1 :(得分: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();
    }

答案 2 :(得分:0)

您的预期值在数组中,并且需要以这种方式访问​​。

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的对象映射。

答案 3 :(得分: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();

}
}