Pergunta

I am trying to use a JSON array structure to populate a dynatree. Perhaps I need to change this structure to facilitate this? Here's the JSON I'm responding to an ajax call:

JSON Array

As you can see, I have an array of testSteps (0 being 001016..., 1 being 001024..., etc). The key of each is the test name. The value is another JSON array of the actual steps (Additional testing..., Order sent..., etc).

I need to get these values so that I can build a dynatree like this:

Test steps tree

I'm running jQuery code to try to get the test names (haven't gotten to the steps after this yet):

$.get('ReportSpecimenProcessingServlet', {specimenNumber: specimenNumber}, function(responseJson) { 
var testingNode = $("#tree").dynatree("getTree").selectKey("testing");
testingNode.removeChildren();
var steps = responseJson.testSteps;
//numTests += steps.length;
$.each(steps, function(data) {
    testingNode.addChild({
      title: data,
      isFolder: true
    });
});  

When I look at the console, I see this for "steps":

[Object { 001016 - Calcium, Serum=2}, Object { 001024 - Phosphorus, Serum=[5]}, Object { 001032 - Glucose, Serum=2},...

and this is what my tree looks like:

Testing tree

Can anyone help me with how to get JSON array values via jQuery? I can get the Dynatree nodes if I can just get the values...

EDIT: Adding some raw JSON:

{"testSteps":[{"001016 - Calcium, Serum":["Additional testing received from LCLS at 17-APR-2014 01:40:37.356399 - status A","Order sent to DI at 17-APR-2014 01:42:00.351891 - status S"]},{"001024 - Phosphorus, Serum":["Additional testing received from LCLS at 17-APR-2014 01:40:37.652686 - status A","Order sent to DI at 17-APR-2014 01:42:00.203879 - status S","Results received from DI at 17-APR-2014 14:16:35.960787 - status P","Results sent to LCLS at 17-APR-2014 14:16:51.497767 - status W","Successful response from LCLS at 17-APR-2014 14:16:57.476592 - status I"]}

Also, I have made progress with new jQuery code:

            $.each(responseJson.testSteps, function() {
            $.each(this, function(k,v) {
                console.log("Key " + k + ' Value ' + v);
                testingNode.addChild({
                  title: k,
                  isFolder: true    //test (steps will not be folders)
                });
            });
        });

which gets me this:

Tree with test names

Foi útil?

Solução

Got it to work (breakthrough after this post) - hope it will help others.

I changed my jQuery code to:

            $.each(responseJson.testSteps, function() {
            $.each(this, function(k,v) {
                console.log("Key " + k + ' Value ' + v);
                var testNode = testingNode.addChild({
                  title: k,
                  isFolder: true    //test (steps will not be folders)
                });
                $.each(v, function(key,val) {
                    testNode.addChild({
                        title: val
                    });
                });
            });

which got me this:

enter image description here

Thanks to those who had comments - I appreciate the quick help.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top