简体   繁体   中英

I want to convert xml string data inside json object to json in java

Below is my json string and i want to convert data of additionalDetails into json string, but i am not able to success.

{
  "docs": 70,
  "size": 250,
  "currentPageNo": 0,
  "recordStartFrom": 0,
  "columnHeader": [
    {
      "id": "0",
      "fieldName": "id",
      "imgName": "",
      "tooltipSrc": "",
    }
    ],
    "data": [
    {
      "Number": "10000",
      "price": "4.75",
      "manfName": "",
      "minOrderQty": "0.00",
      "maxOrderQty": "0",
      "additionalDetails": "<item>CUR:Rupees</item><item>code:one</item>",
    },
    {
      "Number": "10001",
      "price": "1.75",
      "manfName": "",
      "minOrderQty": "0.00",
      "maxOrderQty": "0",
      "additionalDetails": "",
    }
    ]
}   

I am trying to convert additionalDetails data into json string. I try to convert json string to XML content and XML to json string but data of additionalDetails is not converted into json string.

This is not a general solution to the stated problem, but a solution to your problem.

I would use regular expressions along the lines of:

<item>([0-9a-zA-Z]*):([0-9a-zA-Z]*)</item>

If you use this expression in global mode you will match each input. You can parse the string using java.util.regex.Pattern . (See the section on "Groups and capturing" to get the values within.)

You can then build up a com.google.gson.JsonArray and populate the values from the Matcher. This array can then be replaced with the additionalDetails field.

Finally you just write out the result or do whatever you wanted to do with it.

I've prepared some FIDDLE for You using X2JS library: http://code.google.com/p/x2js/

INDEX.HTML:

<body>
    <pre id="pre_id">TEST</pre>
    <button id="go_button">GO</button>
</body>

SCRIPT:

var x2js = new X2JS();

$("#go_button").click(function() {
    var form_data = {
      "docs": 70,
      "size": 250,
      "currentPageNo": 0,
      "recordStartFrom": 0,
      "columnHeader": [
        {
          "id": "0",
          "fieldName": "id",
          "imgName": "",
          "tooltipSrc": "",
        }
        ],
        "data": [
        {
          "Number": "10000",
          "price": "4.75",
          "manfName": "",
          "minOrderQty": "0.00",
          "maxOrderQty": "0",
          "additionalDetails": "<item>CUR:Rupees</item><item>code:one</item>",
        },
        {
          "Number": "10001",
          "price": "1.75",
          "manfName": "",
          "minOrderQty": "0.00",
          "maxOrderQty": "0",
          "additionalDetails": "",
        }
        ]
    };
    for (var i=0; i<form_data.data.length; i++) {
        console.log(x2js.xml_str2json(
            '<additionalDetails>'+
            form_data.data[i].additionalDetails+
            '</additionalDetails>'));
        form_data.data[i].additionalDetails = x2js.xml_str2json(
            '<additionalDetails>'+
            form_data.data[i].additionalDetails+
            '</additionalDetails>').additionalDetails;
    }
    $('#pre_id').html(JSON.stringify(form_data));
});

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