2015-09-14 108 views
0

我面临一个相当讨厌的问题,并且无法在互联网上找到一个好的解决方案。我有一个Android应用程序,可以拉下包含用户信息的大型JSON对象。格式如下:修改JSON对象的键

{ 
    "dateOfBirth": { 
    "day": 25, 
    "month": 3 
    }, 
    "educations": { 
    "_total": 1, 
    "values": [{ 
     "activities": "Music Society, Rugby college team", 
     "degree": "BA Computer Science", 
     "endDate": {"year": 2015}, 
     "fieldOfStudy": "Computer Science", 
     "id": 298659638, 
     "notes": "The courses I enjoyed most are: Object-Oriented Programming, Data Structures and Algorithms, Compilers, Databases, Computer Networks and Computer Security.", 
     "schoolName": "University of Oxford", 
     "startDate": {"year": 2012} 
    }] 
    }, 
    "firstName": "Roland", 
    "id": "-1QlZwe5fx", 
    "languages": { 
    "_total": 2, 
    "values": [ 
     { 
     "id": 1, 
     "language": {"name": "English"} 
     }, 
     { 
     "id": 2, 
     "language": {"name": "Romanian"} 
     } 
    ] 
    }, 
    "lastName": "Batovski", 
    "location": {"country": {"code": "gb"}} 
... 
} 

,我要送这个数据有JSON的特定格式,它预计,也就是说,它预计数据来此格式的服务器:

{ 
​ 
    "userid": "Yohv74kuNm", 
​ 
    "profile": [], 
​ 
    "goalData": [ 
​ 
     "Founders", 
​ 
     "Travel" 
​ 
    ], 
​ 
    "event": "1", 
​ 
    "pin": "", 
​ 
    "_id": "c9lZlvo9SEcIwDBR", 
​ 
    "person": { 
​ 
     "id": { 
​ 
      "text": "Yohv74kuNm" 
​ 
     }, 
​ 
     "languages": { 
​ 
      "total": "2", 
​ 
      "language": [ 
​ 
       { 
​ 
        "id": { 
​ 
         "text": "57739336" 
​ 
        }, 
​ 
        "language": { 
​ 
         "name": { 
​ 
          "text": "English" 
​ 
         } 
​ 
        } 
​ 
       } 
​ 
      ] 
​ 
     }, 
​ 
     "skills": { 
​ 
      "total": "15", 
​ 
      "skill": [ 
​ 
       { 
​ 
        "id": { 
​ 
         "text": "11" 
​ 
        }, 
​ 
        "skill": { 
​ 
         "name": { 
​ 
          "text": "Start-ups" 
​ 
         } 
​ 
        } 
​ 
       }, 
​ 
       { 
​ 
        "id": { 
​ 
         "text": "13" 
​ 
        }, 
​ 
        "skill": { 
​ 
         "name": { 
​ 
          "text": "Entrepreneurship" 
​ 
         } 
​ 
        } 
​ 
       }, 
... } 

很明显,这些对象是完全不同的,但实际上他们拥有相同的数据。我正在寻找一种方法轻松映射到另一个。即从我收到的数据中获取数据,然后创建一个新的JSON对象。请记住,这些对象非常大,并且有很多嵌套值。我尝试使用JavaEE的JsonObjectBuilder,但创建一个对象的大小看起来像很多手动工作(我在下面快速尝试)。

JsonObject jsonString = factory.createObjectBuilder() 
     // add the fields here 
     .add("id", factory.createObjectBuilder() 
      .add("text", this.jsonObject.get("id").getAsString()) 
      .build()) 
     .add("languages", factory.createObjectBuilder() 
       .add("total", this.jsonObject.get("languages").getAsJsonObject().get("_total").getAsString()) 
       .build()) 
     .build() 

感谢您的帮助!

回答

0

我会怎么做:

  • 使用Gson序列化/反序列化JSON/POJO的。
  • 为JSON结构一类结构1.
  • 为JSON结构2.
  • 另一个阶级结构让他们无论是在不同的包,只是为了清楚起见。
  • 使用this tool来自动创建类结构。
  • 编写一个转换器,将从json 1反序列化的java对象转换为与json 2兼容的java对象。

另一种可能性是让你的java对象反映两个json结构中的一个,并使用custom serializer来处理其他json结构。

+0

这就是我想要做的。我面临的问题是写你正在谈论的转换器。鉴于JSON对象的复杂结构,这不会很复杂吗? –

+0

@RolandBatovski是的,它可能会变得相当复杂,这取决于两种json格式在结构上的差异。但至少你会使用(类型安全的)java对象,而不是自己构建json字符串,这会比较乏味和容易出错。 虽然其他人可能会有更好的建议。 – FWeigl

+0

我明白了:)非常感谢!将研究它 –