Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
855 views
in Technique[技术] by (71.8m points)

Converting java object with nested classes to Json

I have been trying to create json from a java object / class that has a nested class. However only the top level class is output in Json. I want to return json from a web services application or alternatively covert json to a java object with nested classes

public class JsonData {
    static String firstName;
    static String lastName;
    static String streetName;
    static String cityName;
    static String stateName;

    static PersonalInfo personalInfo = new PersonalInfo(); 

 //  typical set and get followed by the nested class
     static class PersonalInfo {
     String height;
     String weight;
     String eyeColor;
     String favoriteColor;
     // getter's and setters for this class
     }
   }

    // in a separate method that handles the web service request set the values... 

   @RequestMapping(value = "/json", method = RequestMethod.GET)
public @ResponseBody String sendJsonSample() throws JsonGenerationException {
    ObjectMapper jsonMapper = new ObjectMapper();
    System.out.println("
Received Request for json data what happens now ! 
");
    JsonData jsonData = new JsonData();
    jsonData.setCityName("New York");
    jsonData.setFirstName("Fred");
    jsonData.setLastName("Smith");
    jsonData.setStateName("NY");
    jsonData.setStreetName("Broadway");
    JsonData.personalInfo.setEyeColor("Green");
    JsonData.personalInfo.setFavoriteColor("Yellow");
    JsonData.personalInfo.setHeight("SixFeet");
    JsonData.personalInfo.setWeight("180");

      // now convert to json with the following
      try {
        String json = jsonMapper.writeValueAsString(jsonData);
        return json;
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }       
      return "ERROR"
     }

    // if I use a browser to my controller http://localhost:8080/json the json returned to my web services call only includes the top or outer level class 
    {
      stateName: "NY",
      firstName: "Fred",
      lastName: "Smith",
      streetName: "Broadway",
      cityName: "New York"
    }

    // I would have expected or I want I should say 
    {
     "state": "NY",
     "firstName": "Fred",
     "lastName": "Smith",
     "streetName": "Broadway",
     "cityName": "New York",
     "PersonalInfo": {
       "EyeColor":"Green",
       "FavoriteColor": "Yellow",
       "height":"SixFeet",
       "Weight":"189"
     }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Jackson serializes objects and instance properties. Your class defines all of its members as _class_members -- i.e., static. That means that no matter how many objects are instantiated from your class, there will be only one value for each static property. Every piece of code that sets the value of one of those properties will change it for every other piece of code reading it.

Perhaps you have made your properties static because your PersonalInfo nested class class is static. Remove the static modifier from your PersonalInfo class. Even better, make it a top-level class and remove the static modifier.

An aside: Because your code will be used in a web application, your class will be instantiated by many threads. Using non-final static members in data classes such as this is very, very hazardous. All of your clients will be sharing the same data.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...