简体   繁体   中英

Java: Convert JSON object to Custom JSON

Converted SOAP response to JSON (using API), so right now wanted custom JSON response where delete some attributes and change field names. like below examples.

Example:

{
  "s:Envelope": {
    "xmlns:s": "http://schemas.xmlsoap.org/soap/envelope/",
    "s:Body": {
      "Response": {
        "xmlns": "http://www.tempuri.org/",
        "Result": {
          "xmlns:a": "http://wwww.datacontract.org/",
          "a:CodeSet": [
            {
              "a:FullDescription": {
                "i:nil": true
              },
              "a:AgeFrom": {
                "i:nil": true
              },
              "a:ShortDescription": {
                "i:nil": true
              },
              "a:Code": "111111",
              "a:LongDescription": "Have a Nice Day",
              "a:EffectiveDate": "01/01/2017"
            },
            {
              "a:FullDescription": {
                "i:nil": true
              },
              "a:AgeFrom": {
                "i:nil": true
              },
              "a:ShortDescription": {
                "i:nil": true
              },
              "a:Code": "222222",
              "a:LongDescription": "Long Working Day",
              "a:EffectiveDate": "01/01/2018"
            }
          ]
        }
      }
    }
  }
}

To:

   {
  "data": [
    {
      "CODE": "111111",
      "EFFECTIVE_DATE": "2017-01-01",
      "TERM_DATE": "9999-12-31",
      "AGE_FROM": "true",
      "SHORT_DESC": "HND",
      "LONG_DESC": "Have a Nice Day"
    },
    {
      "CODE": "2222222",
      "EFFECTIVE_DATE": "2018-01-01",
      "TERM_DATE": "9999-12-31",
      "AGE_FROM": "true",
      "SHORT_DESC": "LWD",
      "LONG DESC": "Long Working Day"
    }
  ]
}

Suggestions on easy way to convert it, means any API or annotations which does it? Note: Response time already taking more than 15 secs.

Do as below

  1. Write two classes which map the two json Strings.
  2. Convert the First json String to its Corresponding json object
  3. Write a logic to Map the Object from Step 2 to your second Object
  4. Convert Your second object to JSon String.

Note : There are many ways to convert To/From json Java object From/To Json String, and there are plenty resources online to look into. I left that for you to research what is best for your need.

You could try to use JSON-P 1.1. Here is an example from Adam Bien's blog:

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
//...

@Test
public void jsonObjectUpdate() {
    String key = "update";
    String JAVA_EE_8 = "is easy with Java EE 8";

    JsonObject initial = Json.createObjectBuilder().
        add(key, "was hard in Java EE 7").
        build();

    JsonObject updated = Json.createObjectBuilder(initial).
        add(key, JAVA_EE_8).
        build();

    assertThat(updated.getString(key), is(JAVA_EE_8));
    assertThat(updated.size(), is(1));
}

Full article is here: http://www.adam-bien.com/roller/abien/entry/updating_jsonobjects_with_json_p

Or, if you want a more object-oriented aproach, you should add your own implementation of JsonObject (this is again JSON-P specification). Your implementation would take in the ctor the initial JsonObject and implement logic for extracting and returning that data JsonArray from it.

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