简体   繁体   English

获取 JSON 字符串的列名

[英]Get Column Names Of JSON String

I got this problem when i was trying to get Column list from a JSON String .当我尝试从 JSON String 获取 Column list 时遇到了这个问题。 Im getting JSON String with ajax :我正在使用 ajax 获取 JSON 字符串:

 var jsonData = $.ajax({ method: "GET", url: "read.jsp", data: {title: option}, dataType: "json", async: false }).responseText;

I choose a JSON file from a list and i get it我从列表中选择了一个 JSON 文件,然后我得到了它

and I tried to Get column names with this :我试图用这个来获取列名:

 var newData= JSON.parse(jsonData).["0"]; for(var i = 0; i < newData.length; i++) { var columnsIn = newData[i]; for(var key in columnsIn){ alert(key); } }

The problem is with : ["0"] , when i replace it with the name of the first attribut , it works.问题在于: ["0"] ,当我用第一个属性的名称替换它时,它可以工作。

PS: Json file is not the same. PS:Json文件不一样。

And this is an exemple of A json file :这是一个 json 文件的例子:

    {
    "ASSETMboSet": {
        "rsStart": 0,
        "rsCount": 10,
        "rsTotal": 1262,
        "ASSET": [
            {
                "rowstamp": "[0 0 0 0 2 -100 91 -13]",
                "Attributes": {
                    "ASSETNUM": {
                        "content": "1000"
                    },
                    "SERIALNUM": {
                        "content": "12346"
                    },
                    "VENDOR": {
                        "content": "1001"
                    },
                    "MANUFACTURER": {
                        "content": "1001"
                    },
                    "PURCHASEPRICE": {
                        "content": 0
                    },
                    "REPLACECOST": {
                        "content": 0
                    }, .....

And this is an other exemple because its not the same json file i want to get column names from it :这是另一个示例,因为它与我想从中获取列名的 json 文件不同:

{
    "executionTime": "2018-02-16 05:49:04 AM",
    "stationBeanList": [
        {
            "id": 72,
            "stationName": "W 52 St & 11 Ave",
            "availableDocks": 31,
            "totalDocks": 39,
            "latitude": 40.76727216,
            "longitude": -73.99392888
 },
        {
            "id": 79,
            "stationName": "Franklin St & W Broadway",
            "availableDocks": 19,
            "totalDocks": 33,
            "latitude": 40.71911552,
            "longitude": -74.00666661}`
  1. Do not use async: false .不要使用async: false Ever .曾经

  2. You have to reach "Attributes" and get the Object.keys of that :您必须达到"Attributes"并获得Object.keys

 let data = { "ASSETMboSet": { "rsStart": 0, "rsCount": 10, "rsTotal": 1262, "ASSET": [{ "rowstamp": "[0 0 0 0 2 -100 91 -13]", "Attributes": { "ASSETNUM": { "content": "1000" }, "SERIALNUM": { "content": "12346" }, "VENDOR": { "content": "1001" }, "MANUFACTURER": { "content": "1001" }, "PURCHASEPRICE": { "content": 0 }, "REPLACECOST": { "content": 0 } } }] } // end ASSETMboSet } // end data let keys = Object.keys(data.ASSETMboSet.ASSET[0].Attributes) console.log("keys = ", keys)

The two JSON respones:两个 JSON 响应:

{
"ASSETMboSet": {
    "rsStart": 0,
    "rsCount": 10,
    "rsTotal": 1262,
    "ASSET": [
        {
            "rowstamp": "[0 0 0 0 2 -100 91 -13]",
            "Attributes": {
                "ASSETNUM": {
                    "content": "1000"
                },
                "SERIALNUM": {
                    "content": "12346"
                },
                "VENDOR": {
                    "content": "1001"
                },
                "MANUFACTURER": {
                    "content": "1001"
                },
                "PURCHASEPRICE": {
                    "content": 0
                },
                "REPLACECOST": {
                    "content": 0
                }, .....

And

{ "executionTime": "2018-02-16 05:49:04 AM", 
  "stationBeanList": [ 
    { "id": 72, "stationName": "W 52 St & 11 Ave", "availableDocks": 31, "totalDocks": 39, "latitude": 40.76727216, "longitude": -73.99392888, "statusValue": "In Service", "statusKey": 1, "cationTime": "2018-02-16 05:47:06 AM" }, 
    { "id": 79, "stationName": "W 52 St & 11 Ave", "availableDocks": 31, "totalDocks": 39,

are completely different in their structure.它们的结构完全不同。

So there is no way to automatically detect the column names.所以没有办法自动检测列名。 You need to know the structure of the JSON you request, and based on that information create a function for each of them that looks for the column names.您需要知道您请求的 JSON 的结构,并根据该信息为每个请求创建一个查找列名的函数。

But you can't create a magic function that can extract the column names of any JSON response without knowing how the response looks like.但是你不能创建一个神奇的函数来提取任何JSON 响应的列名而不知道响应的样子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM