将嵌套类的java对象转换为Json [英] Converting java object with nested classes to Json

查看:601
本文介绍了将嵌套类的java对象转换为Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试从具有嵌套类的java对象/类创建json。但是只有顶级类在Json中输出。我想从Web服务应用程序返回json,或者将json转换为具有嵌套类的java对象

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("\nReceived Request for json data what happens now ! \n");
    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"
     }
    }


推荐答案

杰克逊序列化对象和实例属性。您的类将其所有成员定义为_class_members - 即static。这意味着无论从您的类中实例化多少个对象,每个静态属性只有一个值。设置其中一个属性值的每一段代码都会为读取它的每一段代码更改它。

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.

也许你的属性是静态的,因为你的PersonalInfo嵌套类类是静态的。从PersonalInfo类中删除static修饰符。更好的是,使它成为顶级类并删除静态修饰符。

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.

旁白:因为您的代码将在Web应用程序中使用,所以您的类将被实例化由许多线程。在诸如此类的数据类中使用非最终静态成员 非常非常危险 。您的所有客户都将共享相同的数据。

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.

这篇关于将嵌套类的java对象转换为Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆