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

时间:2015-04-18 06:27:29

标签: java json jsonobject

jsonobject转换为Stringlong时,它会返回null。为什么呢?

我的JSON文件:

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

我的代码:

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

输出:

  

id null null,名称为null

3 个答案:

答案 0 :(得分:0)

idname属于project JSONObject,因此请使用project JSONObject

获取这两个值

试试这个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);

}

答案 1 :(得分:0)

问题在于,当您尝试获取idname时,您不是从project获取,而是从包含project的对象获取。应该有:

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

答案 2 :(得分:0)

这种代码可以非常快速地变得非常难看。相反,您可以使用更高阶的解析器,例如Jackson。那么你的代码可以更清晰,你不必担心挖掘每一段JSON的转换。