i using org.codehaus.jackson.JsonGenerator to show my data but i could not be shown in Html

i using org.codehaus.jackson.JsonGenerator to show my data but i could not be shown in Html

peterrezaeepeterrezaee Posts: 6Questions: 1Answers: 0
edited July 2017 in Free community support

i using org.codehaus.jackson.JsonGenerator to show my data but i could not be shown in Html

this is my data
public static ArrayList<String> mactx = new ArrayList<String>(); i use getter and setter.

here is my json

jgen.writeStartObject();
jgen.writeStringField("m",list.getM().toString() );
jgen.writeEndObject();

and my html code

function loadtransfromTable() {
        transfromTable = $('#tableTX').DataTable({
            responsive: true,
            searching: false,
            lengthChange: false,
            scrollX: true,
            paging: false,
           order: [[ 2, 'asc' ], [ 4, 'asc' ]],
            ajax      : {
                url    : "http://" + ipaddress + ":" + restport + "/shown/json",
                dataSrc: ''
            },
            columns   : [
                {data: 'm'}

            ]
        });

        $.ajax({
            url    : "http://" + ipaddress + ":" + restport + "/shown/json",
            success: function (links) {
                console.log(links);
            }
        });
    }

everything works fine, table id is correct, link is correct , name columns too, but html don't show anything when it run
i want show m in my table ,

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,300Questions: 26Answers: 4,769

    What is the actual data return from the ajax request?

    Kevin

  • peterrezaeepeterrezaee Posts: 6Questions: 1Answers: 0
    edited July 2017

    @kthorngren
    it suppose to return list of ip adress like "[192.168.1.13, 192.168.1.14]"
    i think problem is can not read Array
    in commend line :curl http://127.0.0.1:8080/shown/json | python -m json.tool
    data shown as > "m": "[192.168.1.13, 192.168.1.13]",

  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin

    Can you use the debugger and let us know the debug code so we can see the table setup please?

    Allan

  • kthorngrenkthorngren Posts: 20,300Questions: 26Answers: 4,769

    You will need to restructure the returned data so that each array element contains one row. Here is the documentation explaining the data structure for an array of objects:
    https://datatables.net/manual/data/#Objects

    I think it would be best to change the server code to return the structure Datatables is expecting. If you can't do that then you can manipulate the return data to change the structure before applying it to Datatables.

    Kevin

  • peterrezaeepeterrezaee Posts: 6Questions: 1Answers: 0

    @kthorngren
    thanks to you, but it can't not be possible cause rows number uncountable but column are fix, what do you means

    you can manipulate the return data to change the structure before applying it to Datatables ?!

    like change data return Array to Strings,.. ?!

  • kthorngrenkthorngren Posts: 20,300Questions: 26Answers: 4,769
    edited July 2017

    To make sure I understand, you current JSON data looks like this sample showing 2 of your rows:

    {
        "macAddress": "[80:5A:04:CE:7E:0F, 80:5A:04:CE:7E:0F, .....]",
        "ipAddress": "[192.168.1.13, 192.168.1.13, ......]",
        "packets": "[183, 109, ......]",
        "avg_rate": "[1000, 11220......]",
        "avg_signal": "[-60.8566551481, -64.3962165554, ....]",
        "avg_length": "[142.606557377, 97.8348623853, .....]",
        "air_time": "[208.776, 74.8924444444, ......]",
        "init_time": "[1500596708.797881556, 1500596755.057025424, ......]",
        "end_time": "[1500596716.134020493, 1500596765.452714880, .....]"
    }
    

    You are unable to change your server script to provide the rows in this format:

    [
    {
        "macAddress": "80:5A:04:CE:7E:0F",
        "ipAddress": "192.168.1.13",
        "packets": "183",
        "avg_rate": "1000",
        "avg_signal": "60.8566551481",
        "avg_length": "142.606557377",
        "air_time": "208.776",
        "init_time": "1500596708.797881556",
        "end_time": "1500596716.134020493"
    },
    {
        "macAddress": "80:5A:04:CE:7E:0F",
        "ipAddress": "192.168.1.13",
        "packets": "109",
        "avg_rate": "11220",
        "avg_signal": "-64.3962165554",
        "avg_length": "97.8348623853",
        "air_time": "74.8924444444",
        "init_time": "1500596755.057025424",
        "end_time": "1500596765.452714880"
    },
    .......
    ]
    
    

    If you are unable to change your server script then you will need to modify the returned data to match the second format above. One option is to move the ajax request from the Datatables init code and run it before initializing Datatables. This is an example I did for something else but it will show you one way to get the ajax data.
    http://live.datatables.net/fafuyeyu/1/edit

    This line data = JSON.parse(data); is getting the JSON data. In your case you will need to loop through your data and change the structure. There may be built in Javascript functions to do some of the work, I'm not sure.

    Kevin

  • peterrezaeepeterrezaee Posts: 6Questions: 1Answers: 0

    @ i notice my fault about data i change them,but still i have same problem

    http://debug.datatables.net/ulasop
    what this means?

    mDataProp = undefined

  • kthorngrenkthorngren Posts: 20,300Questions: 26Answers: 4,769
    edited July 2017 Answer ✓

    Your JSON format looks ok:

    {
        "macAddress": "80:5A:04:CE:7E:0F",
        "ipAddress": "192.168.1.13",
        "packets": "568",
        "avg_rate": "11220",
        "avg_signal": "-64.3962165554",
        "avg_length": "97.8348623853",
        "air_time": "208.776",
        "init_time": "1500596755.057025424",
        "end_time": "1500596765.452714880"
    }
    

    Except I think you need to put it into an array as shown here:
    https://datatables.net/manual/data/#Objects

    I think it should look like this:

    [
    {
        "macAddress": "80:5A:04:CE:7E:0F",
        "ipAddress": "192.168.1.13",
        "packets": "568",
        "avg_rate": "11220",
        "avg_signal": "-64.3962165554",
        "avg_length": "97.8348623853",
        "air_time": "208.776",
        "init_time": "1500596755.057025424",
        "end_time": "1500596765.452714880"
    }
    ]
    

    Kevin

  • peterrezaeepeterrezaee Posts: 6Questions: 1Answers: 0

    @kthorngren
    thanks so much
    i should add jgen.writeStartArray(); to first of my JSON code
    that make it look like array

    jgen.writeStartArray()
    jgen.writeStartObject();
    jgen.writeStringField("m",list.getM().toString() );
    jgen.writeEndObject();
    jgen.writeEndArray()
    
  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin

    mDataProp = undefined

    Don't worry about that in the debugger. Its a legacy thing for 1.9 support.

    Great to hear you've got it working with Kevin's help :)

    Allan

This discussion has been closed.