简体   繁体   English

将JSONObject转换为字符串,并长返回null

[英]Convert JSONObject into string and long return null

When jsonobject is converted to String or long it returns null . jsonobject转换为Stringlong它返回null Why? 为什么?

My JSON file: 我的JSON文件:

{
    "memberships": [
        {
            "project": {
                "id": 30483134480107,
                "name": "Asana Integrations"
            },
            "section": null
        }
    ]
}

And my code: 而我的代码:

package jsontest;

import java.beans.Statement;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class MoreComplexJson {
    private static final String filePath = "C:\\jsonTestFile.json";

    public static void main(String[] args) {
        try {
            FileReader reader = new FileReader(filePath);
            JSONParser jsonParser = new JSONParser();
            JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);

            JSONArray memberships = (JSONArray) jsonObject.get("memberships");

            for (int z = 0; z < memberships.size(); z++) {
                Iterator m = memberships.iterator();

                // take each value from the json array separately
                while (m.hasNext()) {
                    JSONObject innerObj = (JSONObject) m.next();
                    Long id = (Long) innerObj.get("id");
                    String name = (String) innerObj.get("name");
                    System.out.println("id " + id + " with name " + name);
                }
            }
        }
        catch (FileNotFoundException ex) {

            ex.printStackTrace();
            System.out.println(ex + "");
        }
        catch (IOException ex) {
            ex.printStackTrace();
            ex.printStackTrace();
            System.out.println(ex + "");
        }
        catch (ParseException ex) {
            ex.printStackTrace();
            ex.printStackTrace();
            System.out.println(ex + "");
        }
        catch (NullPointerException ex) {
            ex.printStackTrace();
            ex.printStackTrace();
            System.out.println(ex + "");
        }
    }
}

The output: 输出:

id null with name null id null,名称为null

id and name belongs to the project JSONObject so get those two values using the project JSONObject idname属于project JSONObject因此请使用project JSONObject获取这两个值

Try this for loop 试试这个for loop

for (int z = 0; z < memberships.size(); z++) {
    JSONObject m = (JSONObject) memberships.get(z);
    JSONObject innerObj = (JSONObject) m.get("project");

    // If you want section
    String section = (String) m.get("section");
    System.out.println("section " + section);


    Long id = (Long) innerObj.get("id");
    String name = (String) innerObj.get("name");
    System.out.println("id " + id + " with name " + name);

}

The problem is that when you are trying to get id and name you're not taking it from project but from object that contains project . 问题是,当您尝试获取idname您不是从project获取它,而是从包含project对象中获取它。 There should be: 应该有:

  JSONObject innerObj = (JsonObject) ((JSONObject) m.next()).get("project)";

This kind of code can get pretty ugly pretty fast. 这种代码很快就会变得很丑陋。 Instead you could use a higher order parser, such as Jackson . 相反,您可以使用更高阶的解析器,例如Jackson Then your code can be much cleaner and you don't have to worry about digging into the conversion of each piece of JSON. 这样,您的代码就可以变得更加整洁,您不必担心深入研究每个JSON的转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM