Complex JSON nesting of objects and arrays

前端 未结 5 1659
清歌不尽
清歌不尽 2020-12-23 09:51

I am having difficultly with syntax and structure of JSON objects/arrays.

{ 
  \"accounting\" : [   
                     { \"firstName\" : \"John\",  
              


        
相关标签:
5条回答
  • 2020-12-23 09:58

    I successfully solved my problem. Here is my code:

    The complex JSON object:

       {
        "medications":[{
                "aceInhibitors":[{
                    "name":"lisinopril",
                    "strength":"10 mg Tab",
                    "dose":"1 tab",
                    "route":"PO",
                    "sig":"daily",
                    "pillCount":"#90",
                    "refills":"Refill 3"
                }],
                "antianginal":[{
                    "name":"nitroglycerin",
                    "strength":"0.4 mg Sublingual Tab",
                    "dose":"1 tab",
                    "route":"SL",
                    "sig":"q15min PRN",
                    "pillCount":"#30",
                    "refills":"Refill 1"
                }],
                "anticoagulants":[{
                    "name":"warfarin sodium",
                    "strength":"3 mg Tab",
                    "dose":"1 tab",
                    "route":"PO",
                    "sig":"daily",
                    "pillCount":"#90",
                    "refills":"Refill 3"
                }],
                "betaBlocker":[{
                    "name":"metoprolol tartrate",
                    "strength":"25 mg Tab",
                    "dose":"1 tab",
                    "route":"PO",
                    "sig":"daily",
                    "pillCount":"#90",
                    "refills":"Refill 3"
                }],
                "diuretic":[{
                    "name":"furosemide",
                    "strength":"40 mg Tab",
                    "dose":"1 tab",
                    "route":"PO",
                    "sig":"daily",
                    "pillCount":"#90",
                    "refills":"Refill 3"
                }],
                "mineral":[{
                    "name":"potassium chloride ER",
                    "strength":"10 mEq Tab",
                    "dose":"1 tab",
                    "route":"PO",
                    "sig":"daily",
                    "pillCount":"#90",
                    "refills":"Refill 3"
                }]
            }
        ],
        "labs":[{
            "name":"Arterial Blood Gas",
            "time":"Today",
            "location":"Main Hospital Lab"      
            },
            {
            "name":"BMP",
            "time":"Today",
            "location":"Primary Care Clinic"    
            },
            {
            "name":"BNP",
            "time":"3 Weeks",
            "location":"Primary Care Clinic"    
            },
            {
            "name":"BUN",
            "time":"1 Year",
            "location":"Primary Care Clinic"    
            },
            {
            "name":"Cardiac Enzymes",
            "time":"Today",
            "location":"Primary Care Clinic"    
            },
            {
            "name":"CBC",
            "time":"1 Year",
            "location":"Primary Care Clinic"    
            },
            {
            "name":"Creatinine",
            "time":"1 Year",
            "location":"Main Hospital Lab"  
            },
            {
            "name":"Electrolyte Panel",
            "time":"1 Year",
            "location":"Primary Care Clinic"    
            },
            {
            "name":"Glucose",
            "time":"1 Year",
            "location":"Main Hospital Lab"  
            },
            {
            "name":"PT/INR",
            "time":"3 Weeks",
            "location":"Primary Care Clinic"    
            },
            {
            "name":"PTT",
            "time":"3 Weeks",
            "location":"Coumadin Clinic"    
            },
            {
            "name":"TSH",
            "time":"1 Year",
            "location":"Primary Care Clinic"    
            }
        ],
        "imaging":[{
            "name":"Chest X-Ray",
            "time":"Today",
            "location":"Main Hospital Radiology"    
            },
            {
            "name":"Chest X-Ray",
            "time":"Today",
            "location":"Main Hospital Radiology"    
            },
            {
            "name":"Chest X-Ray",
            "time":"Today",
            "location":"Main Hospital Radiology"    
            }
        ]
    }
    

    The jQuery code to grab the data and display it on my webpage:

    $(document).ready(function() {
    var items = [];
    
    $.getJSON('labOrders.json', function(json) {
      $.each(json.medications, function(index, orders) {
        $.each(this, function() {
            $.each(this, function() {
                items.push('<div class="row">'+this.name+"\t"+this.strength+"\t"+this.dose+"\t"+this.route+"\t"+this.sig+"\t"+this.pillCount+"\t"+this.refills+'</div>'+"\n");
            });
        });
      });
    
      $('<div>', {
        "class":'loaded',
        html:items.join('')
      }).appendTo("body");
    
    });
    

    });

    0 讨论(0)
  • 2020-12-23 10:01

    The first code is an example of Javascript code, which is similar, however not JSON. JSON would not have 1) comments and 2) the var keyword

    You don't have any comments in your JSON, but you should remove the var and start like this:

    orders: {
    

    The [{}] notation means "object in an array" and is not what you need everywhere. It is not an error, but it's too complicated for some purposes. AssociatedDrug should work well as an object:

    "associatedDrug": {
                    "name":"asprin",
                    "dose":"",
                    "strength":"500 mg"
              }
    

    Also, the empty object labs should be filled with something.

    Other than that, your code is okay. You can either paste it into javascript, or use the JSON.parse() method, or any other parsing method (please don't use eval)

    Update 2 answered:

    obj.problems[0].Diabetes[0].medications[0].medicationsClasses[0].className[0].associatedDrug[0].name
    

    returns 'aspirin'. It is however better suited for foreaches everywhere

    0 讨论(0)
  • 2020-12-23 10:01

    Make sure you follow the language definition for JSON. In your second example, the section:

    "labs":[{
        ""
    }]
    

    Is invalid since an object must be composed of zero or more key-value pairs "a" : "b", where "b" may be any valid value. Some parsers may automatically interpret { "" } to be { "" : null }, but this is not a clearly defined case.

    Also, you are using a nested array of objects [{}] quite a bit. I would only do this if:

    1. There is no good "identifier" string for each object in the array.
    2. There is some clear reason for having an array over a key-value for that entry.
    0 讨论(0)
  • 2020-12-23 10:03

    First, choosing a data structure(xml,json,yaml) usually includes only a readability/size problem. For example

    Json is very compact, but no human being can read it easily, very hard do debug,

    Xml is very large, but everyone can easily read/debug it,

    Yaml is in between Xml and json.

    But if you want to work with Javascript heavily and/or your software makes a lot of data transfer between browser-server, you should use Json, because it is pure javascript and very compact. But don't try to write it in a string, use libraries to generate the code you needed from an object.

    Hope this helps.

    0 讨论(0)
  • 2020-12-23 10:10

    You can try use this function to find any object in nested nested array of arrays of kings.

    Example

    function findTByKeyValue (element, target){
            var found = true;
            for(var key in target) { 
                if (!element.hasOwnProperty(key) || element[key] !== target[key])   { 
                    found = false;
                    break;
                }
            }
            if(found) {
                return element;
            }
            if(typeof(element) !== "object") { 
                return false;
            }
            for(var index in element) { 
                var result = findTByKeyValue(element[index],target);
                if(result) { 
                    return result; 
                }
            } 
        };
    
    findTByKeyValue(problems,{"name":"somethingElse","strength":"500 mg"}) =====> result equal to object associatedDrug#2
    
    0 讨论(0)
提交回复
热议问题