最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Parse dynamic nested JSON into HTML table - Stack Overflow

programmeradmin1浏览0评论

My experience with parsing JSON is fairly minimal but the document im working with is pretty big. JSON Objects are nested within one another and the keys are fairly consistent with "title","description","properties","default", and "type". Property/Object names will vary and new values may be added overtime so I want this to be as flexible as possible. Here is a sample of the JSON I am working with, The real document is much larger:

{
"title": "settings schema",
"description": "Settings schema ",
"type": "object",
"properties": {
    "alerts": {
        "description": "Settings for alerts ",
        "type": "object",
        "properties": {
            "pageSize": {
                "description": "The number of alerts .",
                "type": "number",
                "default": 15
            },
            "paramKeys": {
                "description": "parameter keys",
                "type": "string",
                "default": "fromKey,toKey,inKey,outKey"
            },
            "alertsEnabled": {
                "description": "Enable/disable alerts",
                "type": "boolean",
                "default": true
            },
            "actionablesEnabled": {
                "description": "Enable/disable actionable alerts",
                "type": "boolean",
                "default": true
            },
            "HistoryEnabled": {
                "description": "Enable/disable alert history",
                "type": "boolean",
                "default": true
            },
            "generalAlertsEnabled": {
                "description": "Enable/disable general alerts",
                "type": "boolean",
                "default": true
            },
            "accountsEnabled": {
                "description": "Enable/disable account alerts",
                "type": "boolean",
                "default": true
            },
            "alertPrefsEnabled": {
                "description": "Enable/disable alert preferences",
                "type": "boolean",
                "default": true
            },
            "datePicker": {
                "description": "Search date picker settings",
                "type": "object",
                "properties": {
                    "maxSearchDays": {
                        "description": "The maximum days to search before today's date. Used on search page",
                        "type": "integer",
                        "default": 365
                    },
                    "minDays": {
                        "description": "The number of days before a user is able to select a date. Should be less than the maxDays",
                        "type": "integer",
                        "default": 0
                    },
                    "maxDays": {
                        "description": "The total number of days that user is able to select a date until. Should be greater than minDays",
                        "type": "integer",
                        "default": 30
                    },
                    "blackOutDays": {
                        "description": "Days of the week indicated by 0 (Sunday) though 6 (Saturday) that will be blacked out",
                        "type": "array",
                        "default": []
                    },
                    "blackOutDates": {
                        "description": "Date Ranges or individual dates in the following format: ['20 Mar 2014 - 1 May 2014', '28 Apr 2014'] that are blacked out or unselectable on the calendar. Typically holidays. ",
                        "type": "array",
                        "default": []
                    },
                    "isAlertCalendar": {
                        "description": "Configures datepicker to work for alerts dnd ",
                        "type": "boolean",
                        "default": true
                    }
                },
                "required": [
                    "maxSearchDays",
                    "minDays",
                    "maxDays",
                    "blackOutDays",
                    "blackOutDates",
                    "isAlertCalendar"
                ]
            }
        },
        "required": [
            "pageSize",
            "paramKeys"
        ]
    }
}

Ive seen a lot of places online say to iterate over arrays but it seems like im dealing with more nested Objects than arrays. Value/Property names may change so I cant really hardcode any property names. I am trying to pull this data and parse it back into an HTML table ideally leaving empty cells where data doesn't apply. For example the first column would have the "alerts" title and every cell underneath it would be empty until all of its properties had been parsed into the next column with property description/type/sub properties/ and defaults in the following columns again leaving blank values when there is no data to include.

Here is a hardcoded example of what I am trying to achieve

Ive never had to work with such plex dynamic json data before so usually its as easy as chaining together keys to get to values but this i really throwing me through a loop and the output i am producing looks like 200 empty cells with the word "id" repeated 10 times in the middle of it.

Any advice helps!

My experience with parsing JSON is fairly minimal but the document im working with is pretty big. JSON Objects are nested within one another and the keys are fairly consistent with "title","description","properties","default", and "type". Property/Object names will vary and new values may be added overtime so I want this to be as flexible as possible. Here is a sample of the JSON I am working with, The real document is much larger:

{
"title": "settings schema",
"description": "Settings schema ",
"type": "object",
"properties": {
    "alerts": {
        "description": "Settings for alerts ",
        "type": "object",
        "properties": {
            "pageSize": {
                "description": "The number of alerts .",
                "type": "number",
                "default": 15
            },
            "paramKeys": {
                "description": "parameter keys",
                "type": "string",
                "default": "fromKey,toKey,inKey,outKey"
            },
            "alertsEnabled": {
                "description": "Enable/disable alerts",
                "type": "boolean",
                "default": true
            },
            "actionablesEnabled": {
                "description": "Enable/disable actionable alerts",
                "type": "boolean",
                "default": true
            },
            "HistoryEnabled": {
                "description": "Enable/disable alert history",
                "type": "boolean",
                "default": true
            },
            "generalAlertsEnabled": {
                "description": "Enable/disable general alerts",
                "type": "boolean",
                "default": true
            },
            "accountsEnabled": {
                "description": "Enable/disable account alerts",
                "type": "boolean",
                "default": true
            },
            "alertPrefsEnabled": {
                "description": "Enable/disable alert preferences",
                "type": "boolean",
                "default": true
            },
            "datePicker": {
                "description": "Search date picker settings",
                "type": "object",
                "properties": {
                    "maxSearchDays": {
                        "description": "The maximum days to search before today's date. Used on search page",
                        "type": "integer",
                        "default": 365
                    },
                    "minDays": {
                        "description": "The number of days before a user is able to select a date. Should be less than the maxDays",
                        "type": "integer",
                        "default": 0
                    },
                    "maxDays": {
                        "description": "The total number of days that user is able to select a date until. Should be greater than minDays",
                        "type": "integer",
                        "default": 30
                    },
                    "blackOutDays": {
                        "description": "Days of the week indicated by 0 (Sunday) though 6 (Saturday) that will be blacked out",
                        "type": "array",
                        "default": []
                    },
                    "blackOutDates": {
                        "description": "Date Ranges or individual dates in the following format: ['20 Mar 2014 - 1 May 2014', '28 Apr 2014'] that are blacked out or unselectable on the calendar. Typically holidays. ",
                        "type": "array",
                        "default": []
                    },
                    "isAlertCalendar": {
                        "description": "Configures datepicker to work for alerts dnd ",
                        "type": "boolean",
                        "default": true
                    }
                },
                "required": [
                    "maxSearchDays",
                    "minDays",
                    "maxDays",
                    "blackOutDays",
                    "blackOutDates",
                    "isAlertCalendar"
                ]
            }
        },
        "required": [
            "pageSize",
            "paramKeys"
        ]
    }
}

Ive seen a lot of places online say to iterate over arrays but it seems like im dealing with more nested Objects than arrays. Value/Property names may change so I cant really hardcode any property names. I am trying to pull this data and parse it back into an HTML table ideally leaving empty cells where data doesn't apply. For example the first column would have the "alerts" title and every cell underneath it would be empty until all of its properties had been parsed into the next column with property description/type/sub properties/ and defaults in the following columns again leaving blank values when there is no data to include.

Here is a hardcoded example of what I am trying to achieve

Ive never had to work with such plex dynamic json data before so usually its as easy as chaining together keys to get to values but this i really throwing me through a loop and the output i am producing looks like 200 empty cells with the word "id" repeated 10 times in the middle of it.

Any advice helps!

Share Improve this question edited May 21, 2015 at 22:36 John Park asked May 21, 2015 at 19:32 John ParkJohn Park 2902 gold badges8 silver badges25 bronze badges 1
  • this is object literal - not a json - and you can't have a JSON within a JSON , the whole structure is basically a JSON with more objects nested – nicholaswmin Commented May 21, 2015 at 19:40
Add a ment  | 

2 Answers 2

Reset to default 1
  1. You need to know how deep your structure is in order to render x amount
    of sub property columns ( where x is the number of levels )
  2. When parsing an object you need to know the level where that object is so that you may add columns to that row corresponding to that object.
  3. use recursion. Since you know what type you're dealing with you only have to recurse down the properties with the type object.

I honestly tried solving your problem but I keep bumping to the problem that this table is going to look horrific after 3+ levels. I would perhaps rethink how you want the data to be displayed.

If this is some sort of exercise ( i.e. you have to use a table ) I would look into a js template rendering engine that would help you render the x columns. i.e. something like underscore would let you do:

<tr>
  <% for(var i = 0; i < totalNumberOfLevels; i --) { %>
    <td></td>
  <% }; %>

  <td><%- default %></td>
</tr>

Maybe this helps you out?

Convert JSON array to an HTML table in jQuery

(at the download page is also a module listed which supports sub grid creation) http://www.trirand./blog/?page_id=6

发布评论

评论列表(0)

  1. 暂无评论