简体   繁体   中英

Convert JSONObject into string and long return null

When jsonobject is converted to String or long it returns null . Why?

My JSON file:

{
    "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 and name belongs to the project JSONObject so get those two values using the project JSONObject

Try this 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 . 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 . Then your code can be much cleaner and you don't have to worry about digging into the conversion of each piece of JSON.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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