2016-07-05 12 views
-1

ここはjavascriptの私のjsonです。配列に変換したいのですが。私は何をするのJSON文字列化とjson.parse機能を試してみましたが、succeed.Pleaseがjsonをjavascriptの配列に変換するにはどうすればよいですか?

{ 
"1":{ 
    "1":{ 
     "id":"3", 
     "value":{ 
      "json":{ 
       "Temprature":{ 
        "key":10 
       } 
      } 
     }, 
     "date_time":"2016-07-05 11:22:30", 
     "dev_id":"1", 
     "pro_id":"1" 
    }, 
    "2":{ 
     "id":"4", 
     "value":{ 
      "json":{ 
       "Temprature":{ 
        "key":20 
       } 
      } 
     }, 
     "date_time":"2016-07-05 11:24:15", 
     "dev_id":"1", 
     "pro_id":"1" 
    }, 
    "3":{ 
     "id":"5", 
     "value":{ 
      "json":{ 
       "Temprature":{ 
        "key":30 
       } 
      } 
     }, 
     "date_time":"2016-07-05 11:24:22", 
     "dev_id":"1", 
     "pro_id":"1" 
    }, 
    "4":{ 
     "id":"6", 
     "value":{ 
      "json":{ 
       "Temprature":{ 
        "key":40 
       } 
      } 
     }, 
     "date_time":"2016-07-05 11:24:28", 
     "dev_id":"1", 
     "pro_id":"1" 
    }, 
    "5":{ 
     "id":"7", 
      "value":{ 
       "json":{ 
        "Temprature":{ 
         "key":50 
        } 
       } 
      }, 
      "date_time":"2016-07-05 11:24:35", 
      "dev_id":"1", 
      "pro_id":"1" 
    }, 
    "6":{ 
     "id":"8", 
     "value":{ 
      "json":{ 
       "Temprature":{ 
        "key":60 
       } 
      } 
     }, 
     "date_time":"2016-07-05 11:24:42", 
     "dev_id":"1", 
     "pro_id":"1" 
    }, 
    "7":{ 
     "id":"9", 
     "value":{ 
      "json":{ 
       "Temprature":{ 
        "key":70 
       } 
      } 
     }, 
     "date_time":"2016-07-05 11:24:49", 
     "dev_id":"1", 
     "pro_id":"1" 
    }, 
    "8":{ 
     "id":"10", 
     "value":{ 
      "json":{ 
       "Temprature":{ 
        "key":80 
       } 
      } 
     }, 
     "date_time":"2016-07-05 11:24:55", 
     "dev_id":"1", 
     "pro_id":"1" 
    }, 
    "9":{ 
     "id":"11", 
     "value":{ 
      "json":{ 
       "Temprature":{ 
        "key":90 
       } 
      } 
     }, 
     "date_time":"2016-07-05 11:25:02", 
     "dev_id":"1", 
     "pro_id":"1" 
    }, 
    "10":{ 
     "id":"12", 
     "value":{ 
      "json":{ 
       "Temprature":{ 
        "key":100 
       } 
      } 
     }, 
     "date_time":"2016-07-05 11:25:09", 
     "dev_id":"1", 
     "pro_id":"1" 
    } 
}, 
"2":{ 
    "11":{ 
     "id":"13", 
     "value":{ 
      "json":{ 
       "Temprature":{ 
        "key":20 
       } 
      } 
     }, 
     "date_time":"2016-07-05 11:28:29", 
     "dev_id":"2", 
     "pro_id":"1" 
    }, 
    "12":{ 
     "id":"14", 
     "value":{ 
      "json":{ 
       "Temprature":{ 
        "key":35 
       } 
      } 
     }, 
     "date_time":"2016-07-05 11:28:40", 
     "dev_id":"2", 
     "pro_id":"1" 
    }, 
    "13":{ 
     "id":"15", 
     "value":{ 
      "json":{ 
       "Temprature":{ 
        "key":55 
       } 
      } 
     }, 
     "date_time":"2016-07-05 11:28:47", 
     "dev_id":"2", 
     "pro_id":"1" 
    }, 
    "14":{ 
     "id":"16", 
     "value":{ 
      "json":{ 
       "Temprature":{ 
        "key":39 
       } 
      } 
     }, 
     "date_time":"2016-07-05 11:28:57", 
     "dev_id":"2", 
     "pro_id":"1" 
    }, 
    "15":{ 
     "id":"17", 
     "value":{ 
      "json":{ 
       "Temprature":{ 
        "key":49 
       } 
      } 
     }, 
     "date_time":"2016-07-05 11:29:10", 
     "dev_id":"2", 
     "pro_id":"1" 
    }, 
    "16":{ 
     "id":"18", 
     "value":{ 
      "json":{ 
       "Temprature":{ 
        "key":15 
       } 
      } 
     }, 
     "date_time":"2016-07-05 11:29:19", 
     "dev_id":"2", 
     "pro_id":"1" 
    }, 
    "17":{ 
     "id":"19", 
     "value":{ 
      "json":{ 
       "Temprature":{ 
        "key":28 
       } 
      } 
     }, 
     "date_time":"2016-07-05 11:29:29", 
     "dev_id":"2", 
     "pro_id":"1" 
    }, 
    "18":{ 
     "id":"20", 
     "value":{ 
      "json":{ 
       "Temprature":{ 
        "key":17 
       } 
      } 
     }, 
     "date_time":"2016-07-05 11:29:42", 
     "dev_id":"2", 
     "pro_id":"1" 
    }, 
    "19":{ 
     "id":"21", 
     "value":{ 
      "json":{ 
       "Temprature":{ 
        "key":34 
       } 
      } 
     }, 
     "date_time":"2016-07-05 11:30:11", 
     "dev_id":"2", 
     "pro_id":"1" 
    }, 
    "20":{ 
     "id":"22", 
     "value":{ 
      "json":{ 
       "Temprature":{ 
        "key":47 
       } 
      } 
     }, 
     "date_time":"2016-07-05 11:30:17", 
     "dev_id":"2", 
     "pro_id":"1" 
    } 
} 

} 所望の出力iは配列に変換したい

Array 
(
    [1] => Array 
     (
      [1] => stdClass Object 
       (
        [id] => 3 
        [value] => Array 
         (
          [json] => Array 
           (
            [Temprature] => Array 
             (
              [key] => 10 
             ) 

           ) 

         ) 

        [date_time] => 2016-07-05 11:22:30 
        [dev_id] => 1 
        [pro_id] => 1 
       ) 

      [2] => stdClass Object 
       (
        [id] => 4 
        [value] => Array 
         (
          [json] => Array 
           (
            [Temprature] => Array 
             (
              [key] => 20 
             ) 

           ) 

         ) 

        [date_time] => 2016-07-05 11:24:15 
        [dev_id] => 1 
        [pro_id] => 1 
       ) 

      [3] => stdClass Object 
       (
        [id] => 5 
        [value] => Array 
         (
          [json] => Array 
           (
            [Temprature] => Array 
             (
              [key] => 30 
             ) 

           ) 

         ) 

        [date_time] => 2016-07-05 11:24:22 
        [dev_id] => 1 
        [pro_id] => 1 
       ) 

      [4] => stdClass Object 
       (
        [id] => 6 
        [value] => Array 
         (
          [json] => Array 
           (
            [Temprature] => Array 
             (
              [key] => 40 
             ) 

           ) 

         ) 

        [date_time] => 2016-07-05 11:24:28 
        [dev_id] => 1 
        [pro_id] => 1 
       ) 

      [5] => stdClass Object 
       (
        [id] => 7 
        [value] => Array 
         (
          [json] => Array 
           (
            [Temprature] => Array 
             (
              [key] => 50 
             ) 

           ) 

         ) 

        [date_time] => 2016-07-05 11:24:35 
        [dev_id] => 1 
        [pro_id] => 1 
       ) 

      [6] => stdClass Object 
       (
        [id] => 8 
        [value] => Array 
         (
          [json] => Array 
           (
            [Temprature] => Array 
             (
              [key] => 60 
             ) 

           ) 

         ) 

        [date_time] => 2016-07-05 11:24:42 
        [dev_id] => 1 
        [pro_id] => 1 
       ) 

      [7] => stdClass Object 
       (
        [id] => 9 
        [value] => Array 
         (
          [json] => Array 
           (
            [Temprature] => Array 
             (
              [key] => 70 
             ) 

           ) 

         ) 

        [date_time] => 2016-07-05 11:24:49 
        [dev_id] => 1 
        [pro_id] => 1 
       ) 

      [8] => stdClass Object 
       (
        [id] => 10 
        [value] => Array 
         (
          [json] => Array 
           (
            [Temprature] => Array 
             (
              [key] => 80 
             ) 

           ) 

         ) 

        [date_time] => 2016-07-05 11:24:55 
        [dev_id] => 1 
        [pro_id] => 1 
       ) 

      [9] => stdClass Object 
       (
        [id] => 11 
        [value] => Array 
         (
          [json] => Array 
           (
            [Temprature] => Array 
             (
              [key] => 90 
             ) 

           ) 

         ) 

        [date_time] => 2016-07-05 11:25:02 
        [dev_id] => 1 
        [pro_id] => 1 
       ) 

      [10] => stdClass Object 
       (
        [id] => 12 
        [value] => Array 
         (
          [json] => Array 
           (
            [Temprature] => Array 
             (
              [key] => 100 
             ) 

           ) 

         ) 

        [date_time] => 2016-07-05 11:25:09 
        [dev_id] => 1 
        [pro_id] => 1 
       ) 

     ) 

    [2] => Array 
     (
      [11] => stdClass Object 
       (
        [id] => 13 
        [value] => Array 
         (
          [json] => Array 
           (
            [Temprature] => Array 
             (
              [key] => 20 
             ) 

           ) 

         ) 

        [date_time] => 2016-07-05 11:28:29 
        [dev_id] => 2 
        [pro_id] => 1 
       ) 

      [12] => stdClass Object 
       (
        [id] => 14 
        [value] => Array 
         (
          [json] => Array 
           (
            [Temprature] => Array 
             (
              [key] => 35 
             ) 

           ) 

         ) 

        [date_time] => 2016-07-05 11:28:40 
        [dev_id] => 2 
        [pro_id] => 1 
       ) 

      [13] => stdClass Object 
       (
        [id] => 15 
        [value] => Array 
         (
          [json] => Array 
           (
            [Temprature] => Array 
             (
              [key] => 55 
             ) 

           ) 

         ) 

        [date_time] => 2016-07-05 11:28:47 
        [dev_id] => 2 
        [pro_id] => 1 
       ) 

      [14] => stdClass Object 
       (
        [id] => 16 
        [value] => Array 
         (
          [json] => Array 
           (
            [Temprature] => Array 
             (
              [key] => 39 
             ) 

           ) 

         ) 

        [date_time] => 2016-07-05 11:28:57 
        [dev_id] => 2 
        [pro_id] => 1 
       ) 

      [15] => stdClass Object 
       (
        [id] => 17 
        [value] => Array 
         (
          [json] => Array 
           (
            [Temprature] => Array 
             (
              [key] => 49 
             ) 

           ) 

         ) 

        [date_time] => 2016-07-05 11:29:10 
        [dev_id] => 2 
        [pro_id] => 1 
       ) 

      [16] => stdClass Object 
       (
        [id] => 18 
        [value] => Array 
         (
          [json] => Array 
           (
            [Temprature] => Array 
             (
              [key] => 15 
             ) 

           ) 

         ) 

        [date_time] => 2016-07-05 11:29:19 
        [dev_id] => 2 
        [pro_id] => 1 
       ) 

      [17] => stdClass Object 
       (
        [id] => 19 
        [value] => Array 
         (
          [json] => Array 
           (
            [Temprature] => Array 
             (
              [key] => 28 
             ) 

           ) 

         ) 

        [date_time] => 2016-07-05 11:29:29 
        [dev_id] => 2 
        [pro_id] => 1 
       ) 

      [18] => stdClass Object 
       (
        [id] => 20 
        [value] => Array 
         (
          [json] => Array 
           (
            [Temprature] => Array 
             (
              [key] => 17 
             ) 

           ) 

         ) 

        [date_time] => 2016-07-05 11:29:42 
        [dev_id] => 2 
        [pro_id] => 1 
       ) 

      [19] => stdClass Object 
       (
        [id] => 21 
        [value] => Array 
         (
          [json] => Array 
           (
            [Temprature] => Array 
             (
              [key] => 34 
             ) 

           ) 

         ) 

        [date_time] => 2016-07-05 11:30:11 
        [dev_id] => 2 
        [pro_id] => 1 
       ) 

      [20] => stdClass Object 
       (
        [id] => 22 
        [value] => Array 
         (
          [json] => Array 
           (
            [Temprature] => Array 
             (
              [key] => 47 
             ) 

           ) 

         ) 

        [date_time] => 2016-07-05 11:30:17 
        [dev_id] => 2 
        [pro_id] => 1 
       ) 

     ) 

) 

する必要がありますを助けるdidnotています。 どうすればいいですか? はありますか?

ありがとうございました

+4

所望の出力がどのように私たちを表示します。 –

+0

JSONで表現された配列がないので、解析してデータを操作する必要があります。どのようにするかは、どのフォーマットの配列を使用するかによって異なります。 – Quentin

+0

どのプロパティを配列に含める必要がありますか? {1:{1:{}、2:{}}、2:{11:{}、12:{}}] '? –

答えて

2

キーを省略することもできます。

var data = { "1": { "1": { id: "3", value: { json: { Temprature: { key: 10 } } }, date_time: "2016-07-05 11:22:30", dev_id: "1", pro_id: "1" }, "2": { id: "4", value: { json: { Temprature: { key: 20 } } }, date_time: "2016-07-05 11:24:15", dev_id: "1", pro_id: "1" }, "3": { id: "5", value: { json: { Temprature: { key: 30 } } }, date_time: "2016-07-05 11:24:22", dev_id: "1", pro_id: "1" }, "4": { id: "6", value: { json: { Temprature: { key: 40 } } }, date_time: "2016-07-05 11:24:28", dev_id: "1", pro_id: "1" }, "5": { id: "7", value: { json: { Temprature: { key: 50 } } }, date_time: "2016-07-05 11:24:35", dev_id: "1", pro_id: "1" }, "6": { id: "8", value: { json: { Temprature: { key: 60 } } }, date_time: "2016-07-05 11:24:42", dev_id: "1", pro_id: "1" }, "7": { id: "9", value: { json: { Temprature: { key: 70 } } }, date_time: "2016-07-05 11:24:49", dev_id: "1", pro_id: "1" }, "8": { id: "10", value: { json: { Temprature: { key: 80 } } }, date_time: "2016-07-05 11:24:55", dev_id: "1", pro_id: "1" }, "9": { id: "11", value: { json: { Temprature: { key: 90 } } }, date_time: "2016-07-05 11:25:02", dev_id: "1", pro_id: "1" }, "10": { id: "12", value: { json: { Temprature: { key: 100 } } }, date_time: "2016-07-05 11:25:09", dev_id: "1", pro_id: "1" } }, "2": { "11": { id: "13", value: { json: { Temprature: { key: 20 } } }, date_time: "2016-07-05 11:28:29", dev_id: "2", pro_id: "1" }, "12": { id: "14", value: { json: { Temprature: { key: 35 } } }, date_time: "2016-07-05 11:28:40", dev_id: "2", pro_id: "1" }, "13": { id: "15", value: { json: { Temprature: { key: 55 } } }, date_time: "2016-07-05 11:28:47", dev_id: "2", pro_id: "1" }, "14": { id: "16", value: { json: { Temprature: { key: 39 } } }, date_time: "2016-07-05 11:28:57", dev_id: "2", pro_id: "1" }, "15": { id: "17", value: { json: { Temprature: { key: 49 } } }, date_time: "2016-07-05 11:29:10", dev_id: "2", pro_id: "1" }, "16": { id: "18", value: { json: { Temprature: { key: 15 } } }, date_time: "2016-07-05 11:29:19", dev_id: "2", pro_id: "1" }, "17": { id: "19", value: { json: { Temprature: { key: 28 } } }, date_time: "2016-07-05 11:29:29", dev_id: "2", pro_id: "1" }, "18": { id: "20", value: { json: { Temprature: { key: 17 } } }, date_time: "2016-07-05 11:29:42", dev_id: "2", pro_id: "1" }, "19": { id: "21", value: { json: { Temprature: { key: 34 } } }, date_time: "2016-07-05 11:30:11", dev_id: "2", pro_id: "1" }, "20": { id: "22", value: { json: { Temprature: { key: 47 } } }, date_time: "2016-07-05 11:30:17", dev_id: "2", pro_id: "1" } } }, 
 
    array = Object.keys(data).map(function (k) { 
 
     return Object.keys(data[k]).map(function (l) { 
 
      return data[k][l]; 
 
     }); 
 
    }); 
 

 
console.log(array);

それとも、キーを保つことができます。

var data = { "1": { "1": { id: "3", value: { json: { Temprature: { key: 10 } } }, date_time: "2016-07-05 11:22:30", dev_id: "1", pro_id: "1" }, "2": { id: "4", value: { json: { Temprature: { key: 20 } } }, date_time: "2016-07-05 11:24:15", dev_id: "1", pro_id: "1" }, "3": { id: "5", value: { json: { Temprature: { key: 30 } } }, date_time: "2016-07-05 11:24:22", dev_id: "1", pro_id: "1" }, "4": { id: "6", value: { json: { Temprature: { key: 40 } } }, date_time: "2016-07-05 11:24:28", dev_id: "1", pro_id: "1" }, "5": { id: "7", value: { json: { Temprature: { key: 50 } } }, date_time: "2016-07-05 11:24:35", dev_id: "1", pro_id: "1" }, "6": { id: "8", value: { json: { Temprature: { key: 60 } } }, date_time: "2016-07-05 11:24:42", dev_id: "1", pro_id: "1" }, "7": { id: "9", value: { json: { Temprature: { key: 70 } } }, date_time: "2016-07-05 11:24:49", dev_id: "1", pro_id: "1" }, "8": { id: "10", value: { json: { Temprature: { key: 80 } } }, date_time: "2016-07-05 11:24:55", dev_id: "1", pro_id: "1" }, "9": { id: "11", value: { json: { Temprature: { key: 90 } } }, date_time: "2016-07-05 11:25:02", dev_id: "1", pro_id: "1" }, "10": { id: "12", value: { json: { Temprature: { key: 100 } } }, date_time: "2016-07-05 11:25:09", dev_id: "1", pro_id: "1" } }, "2": { "11": { id: "13", value: { json: { Temprature: { key: 20 } } }, date_time: "2016-07-05 11:28:29", dev_id: "2", pro_id: "1" }, "12": { id: "14", value: { json: { Temprature: { key: 35 } } }, date_time: "2016-07-05 11:28:40", dev_id: "2", pro_id: "1" }, "13": { id: "15", value: { json: { Temprature: { key: 55 } } }, date_time: "2016-07-05 11:28:47", dev_id: "2", pro_id: "1" }, "14": { id: "16", value: { json: { Temprature: { key: 39 } } }, date_time: "2016-07-05 11:28:57", dev_id: "2", pro_id: "1" }, "15": { id: "17", value: { json: { Temprature: { key: 49 } } }, date_time: "2016-07-05 11:29:10", dev_id: "2", pro_id: "1" }, "16": { id: "18", value: { json: { Temprature: { key: 15 } } }, date_time: "2016-07-05 11:29:19", dev_id: "2", pro_id: "1" }, "17": { id: "19", value: { json: { Temprature: { key: 28 } } }, date_time: "2016-07-05 11:29:29", dev_id: "2", pro_id: "1" }, "18": { id: "20", value: { json: { Temprature: { key: 17 } } }, date_time: "2016-07-05 11:29:42", dev_id: "2", pro_id: "1" }, "19": { id: "21", value: { json: { Temprature: { key: 34 } } }, date_time: "2016-07-05 11:30:11", dev_id: "2", pro_id: "1" }, "20": { id: "22", value: { json: { Temprature: { key: 47 } } }, date_time: "2016-07-05 11:30:17", dev_id: "2", pro_id: "1" } } }, 
 
    array = Object.keys(data).map(function (k) { 
 
     var o = data[k], 
 
      q = {}; 
 
     q[k] = Object.keys(o).map(function (l) { 
 
      var p = {}; 
 
      p[l] = o[l]; 
 
      return p; 
 
     }); 
 
     return q; 
 
    }); 
 

 
console.log(array);

関連する問題