在R中格式化JSON字符串

时间:2015-07-01 22:27:17

标签: json r

我将下面的字符串作为API的响应(我省略了大部分内容):

"{\"Results\":{\"output1\":{\"type\":\"table\",\"value\":{\"ColumnNames\":[\"X\",\"Y\",\"month\",\"day\",\"FFMC\",\"DMC\",\"DC\",\"ISI\",\"temp\",\"RH\",\"wind\",\"rain\",\"area\",\"classes\",\"probabilities\"],\"ColumnTypes\":[\"Int32\",\"Int32\",\"String\",\"String\",\"Double\",\"Double\",\"Double\",\"Double\",\"Double\",\"Int32\",\"Double\",\"Double\",\"Double\",\"String\",\"Double\"],\"Values\":[[\"7\",\"5\",\"mar\",\"fri\",\"86.2\",\"26.2\",\"94.3\",\"5.1\",\"8.2\",\"51\",\"6.7\",\"0\",\"0\",\"0\",\"0.000994054357184526\"],[\"7\",\"4\",\"oct\",\"tue\",\"90.6\",\"35.4\",\"669.1\",\"6.7\",\"18\",\"33\",\"0.9\",\"0\",\"0\",\"0\",\"0.0984320195347124\"],[\"7\",\"4\",\"oct\",\"sat\",\"90.6\",\"43.7\",\"686.9\",\"6.7\",\"14.6\",\"33\",\"1.3\",\"0\",\"0\",\"0\",\"0.0691718166965817\"],[\"8\",\"6\",\"mar\",\"fri\",\"91.7\",\"33.3\",\"77.5\",\"9\",\"8.3\",\"97\",\"4\",\"0.2\",\"0\",\"0\",\"2.95310265990751E-05\"],[\"8\",\"6\",\"mar\",\"sun\",\"89.3\",\"51.3\",\"102.2\",\"9.6\",\"11.4\",\"99\",\"1.8\",\"0\",\"0\",\"0\",\"0.000481037981016765\"],[\"8\",\"6\",\"aug\",\"sun\",\"92.3\",\"85.3\",\"488\",\"14.7\",\"22.2\",\"29\",\"5.4\",\"0\",\"0\",\"0\",\"0.209205163047233\"],[\"8\",\"6\",\"aug\",\"mon\",\"92.3\",\"88.9\",\"495.6\",\"8.5\",\"24.1\",\"27\",\"3.1\",\"0\",\"0\",\"0\",\"0.213748527104477\"],[\"8\",\"6\",\"aug\",\"mon\",\"91.5\",\"145.4\",\"608.2\",\"10.7\",\"8\",\"86\",\"2.2\",\"0\",\"0\",\"0\",\"0.0252264012644276\"],[\"8\",\"6\",\"sep\",\"tue\",\"91\",\"129.5\",\"692.6\",\"7\",\"13.1\",\"63\",\"5.4\",\"0\",\"0\",\"0\",\"0.0786459836705183\"],[\"7\",\"5\",\"sep\",\"sat\",\"92.5\",\"88\",\"698.6\",\"7.1\",\"22.8\",\"40\",\"4\",\"0\",\"0\",\"0\",\"0.194239272297234\"],[\"7\",\"5\",\"sep\",\"sat\",\"92.5\",\"88\",\"698.6\",\"7.1\",\"17.8\",\"51\",\"7.2\",\"0\",\"0\",\"0\",\"0.137307723043153\"],[\"7\",\"5\",\"sep\",\"sat\",\"92.8\",\"73.2\",\"713\",\"22.6\",\"19.3\",\"38\",\"4\",\"0\",\"0\",\"0\",\"0.402462581333792\"],[\"6\",\"5\",\"aug\",\"fri\",\"63.5\",\"70.8\",\"665.3\",\"0.8\",\"17\",\"72\",\"6.7\",\"0\",\"0\",\"0\",\"8.13136998645888E-19\"],[\"6\",\"5\",\"sep\",\"mon\",\"90.9\",\"126.5\",\"686.5\",\"7\",\"21.3\",\"42\",\"2.2\",\"0\",\"0\",\"0\",\"0.241307820038569\"],[\"6\",\"5\",\"sep\",\"wed\",\"92.9\",\"133.3\",\"699.6\",\"9.2\",\"26.4\",\"21\",\"4.5\",\"0\",\"0\",\"0\",\"0.179696441434139\"],[\"6\",\"5\",\"sep\",\"fri\",\"93.3\",\"141.2\",\"713.9\",\"13.9\",\"22.9\",\"44\",\"5.4\",\"0\",\"0\",\"0\",\"0.189071717813771\"],[\"5\",\"5\",\"mar\",\"sat\",\"91.7\",\"35.8\",\"80.8\",\"7.8\",\"15.1\",\"27\",\"5.4\",\"0\",\"0\",\"0\",\"0.0211889454624805\"],[\"8\",\"5\",\"oct\",\"mon\",\"84.9\",\"32.8\",\"664.2\",\"3\",\"16.7\",\"47\",\"4.9\",\"0\",\"0\",\"0\",\"0.0362362138274161\"],[\"6\",\"4\",\"mar\",\"wed\",\"89.2\",\"27.9\",\"70.8\",\"6.3\",\"15.9\",\"35\",\"4\",\"0\",\"0\",\"0\",\"0.0218392837814828\"],[\"6\",\"4\",\"apr\",\"sat\",\"86.3\",\"27.4\",\"97.1\",\"5.1\",\"9.3\",\"44\",\"4.5\",\"0\",\"0\",\"0\",\"0.00324573096926237\"],[\"6\",\"4\",\"sep\",\"tue\",\"91\",\"129.5\",\"692.6\",\"7\",\"18.3\",\"40\",\"2.7\",\"0\",\"0\",\"0\",\"0.0851998179097373\"],[\"5\",\"4\",\"sep\",\"mon\",\"91.8\",\"78.5\",\"724.3\",\"9.2\",\"19.1\",\"38\",\"2.7\",\"0\",\"0\",\"0\",\"0.0827146227090543\"],[\"7\",\"4\",\"jun\",\"sun\",\"94.3\",\"96.3\",\"200\",\"56.1\",\"21\",\"44\",\"4.5\",\"0\",\"0\",\"0\",\"8.12683713838986E-18\"],[\"7\",\"4\",\"aug\",\"sat\",\"90.2\",\"110.9\",\"537.4\",\"6.2\",\"19.5\",\"43\",\"5.8\",\"0\",\"0\",\"0\",\"0.093496218523508\"],[\"7\",\"4\",\"aug\",\"sat\",\"93.5\",\"139.4\",\"594.2\",\"20.3\",\"23.7\",\"32\",\"5.8\",\"0\",\"0\",\"0\",\"0.234891194199846\"],[\"7\",\"4\",\"aug\",\"sun\",\"91.4\",\"142.4\",\"601.4\",\"10.6\",\"16.3\",\"60\",\"5.4\",\"0\",\"0\",\"0\",\"0.0315637936260445\"],[\"7\",\"4\",\"sep\",\"fri\",\"92.4\",\"117.9\",\"668\",\"12.2\",\"19\",\"34\",\"5.8\",\"0\",\"0\",\"0\",\"0.0763483073969382\"],[\"7\",\"4\",\"sep\",\"mon\",\"90.9\",\"126.5\",\"686.5\",\"7\",\"19.4\",\"48\",\"1.3\",\"0\",\"0\",\"0\",\"0.0807082768380801\"],[\"6\",\"3\",\"sep\",\"sat\",\"93.4\",\"145.4\",\"721.4\",\"8.1\",\"30.2\",\"24\",\"2.7\",\"0\",\"0\",\"0\",\"0.123496836192976\"],[\"6\",\"3\",\"sep\",\"sun\",\"93.5\",\"149.3\",\"728.6\",\"8.1\",\"22.8\",\"39\",\"3.6\",\"0\",\"0\",\"0\",\"0.104470815098067\"],[\"6\",\"3\",\"sep\",\"fri\",\"94.3\",\"85.1\",\"692.3\",\"15.9\",\"25.4\",\"24\",\"3.6\",\"0\",\"0\",\"0\",\"0.484699006196703\"],[\"6\",\"3\",\"sep\",\"mon\",\"88.6\",\"91.8\",\"709.9\",\"7.1\",\"11.2\",\"78\",\"7.6\",\"0\",\"0\",\"0\",\"0.0402495952863102\"],[\"6\",\"3\",\"sep\",\"fri\",\"88.6\",\"69.7\",\"706.8\",\"5.8\",\"20.6\",\"37\",\"1.8\",\"0\",\"0\",\"1\",\"0.544054727393601\"],[\"6\",\"3\",\"sep\",\"sun\",\"91.7\",\"75.6\",\"718.3\",\"7.8\",\"17.7\",\"39\",\"3.6\",\"0\",\"0\",\"0\",\"0.24271814159079\"],[\"6\",\"3\",\"sep\",\"mon\",\"91.8\",\"78.5\",\"724.3\",\"9.2\",\"21.2\",\"32\",\"2.7\",\"0\",\"0\",\"0\",\"0.222235424268343\"],[\"6\",\"3\",\"sep\",\"tue\",\"90.3\",\"80.7\",\"730.2\",\"6.3\",\"18.2\",\"62\",\"4.5\",\"0\",\"0\",\"0\",\"0.270332249530051\"],[\"6\",\"3\",\"oct\",\"tue\",\"90.6\",\"35.4\",\"669.1\",\"6.7\",\"21.7\",\"24\",\"4.5\",\"0\",\"0\",\"0\",\"0.39058831877231\"],[\"7\",\"4\",\"oct\",\"fri\",\"90\",\"41.5\",\"682.6\",\"8.7\",\"11.3\",\"60\",\"5.4\",\"0\",\"0\",\"0\",\"0.0157330746945428\"],[\"7\",\"3\",\"oct\",\"sat\",\"90.6\",\"43.7\",\"686.9\",\"6.7\",\"17.8\",\"27\",\"4\",\"0\",\"0\",\"0\",\"0.361404648767495\"],[\"4\",\"4\",\"mar\",\"tue\",\"88.1\",\"25.7\",\"67.6\",\"3.8\",\"14.1\",\"43\",\"2.7\",\"0\",\"0\",\"0\",\"0.0152093303721934\"],[\"4\",\"4\",\"jul\",\"tue\",\"79.5\",\"60.6\",\"366.7\",\"1.5\",\"23.3\",\"37\",\"3.1\",\"0\",\"0\",\"0\",\"0.00189872892303056\"],[\"4\",\"4\",\"aug\",\"sat\",\"90.2\",\"96.9\",\"624.2\",\"8.9\",\"18.4\",\"42\",\"6.7\",\"0\",\"0\",\"0\",\"0.0714247069312418\"],[\"4\",\"4\",\"aug\",\"tue\",\"94.8\",\"108.3\",\"647.1\",\"17\",\"16.6\",\"54\",\"5.4\",\"0\",\"0\",\"0\",\"0.0872046098917447\"],[\"4\",\"4\",\"sep\",\"sat\",\"92.5\",\"88\",\"698.6\",\"7.1\",\"19.6\",\"48\",\"2.7\",\"0\",\"0\",\"0\",\"0.0779550498175619\"],[\"4\",\"4\",\"sep\",\"wed\",\"90.1\",\"82.9\",\"735.7\",\"6.2\",\"12.9\",\"74\",\"4.9\",\"0\",\"0\",\"0\",\"0.00888810132402561\"],[\"5\",\"6\",\"sep\",\"wed\",\"94.3\",\"85.1\",\"692.3\",\"15.9\",\"25.9\",\"24\",\"4\",\"0\",\"0\",\"0\",\"0.300986370807065\"],[\"5\",\"6\",\"sep\",\"mon\",\"90.9\",\"126.5\"

我需要以这种格式对其进行格式化(只有一个示例,不包含任何值):

{
  "Results": {
    "output1": {
      "type": "DataTable",
      "value": {
        "ColumnNames": [
          "X",
          "Y",
          "month",
          "day",
          "FFMC",
          "DMC",
          "DC",
          "ISI",
          "temp",
          "RH",
          "wind",
          "rain",
          "area",
          "classes",
          "probabilities"
        ],
        "ColumnTypes": [
          "Numeric",
          "Numeric",
          "String",
          "String",
          "Numeric",
          "Numeric",
          "Numeric",
          "Numeric",
          "Numeric",
          "Numeric",
          "Numeric",
          "Numeric",
          "Numeric",
          "Categorical",
          "Numeric"
        ],
        "Values": [
          [
            "0",
            "0",
            "value",
            "value",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0"
          ],
          [
            "0",
            "0",
            "value",
            "value",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0"
          ]
        ]
      }
    }
  }
}

当我使用fromJSON()时,我得到一个非常奇怪的格式,如下所示:

$Results$output1$value$Values[[52]]
 [1] "4"                 "3"                 "aug"               "sun"               "90.2"              "99.6"             
 [7] "631.2"             "6.3"               "21.5"              "34"                "2.2"               "0"                
[13] "0"                 "0"                 "0.392881606323441"

$Results$output1$value$Values[[53]]
 [1] "4"                 "3"                 "aug"               "wed"               "92.1"              "111.2"            
 [7] "654.1"             "9.6"               "20.4"              "42"                "4.9"               "0"                
[13] "0"                 "0"                 "0.172951385719202"

$Results$output1$value$Values[[54]]
 [1] "4"                 "3"                 "aug"               "wed"               "92.1"              "111.2"            
 [7] "654.1"             "9.6"               "20.4"              "42"                "4.9"               "0"                
[13] "0"                 "0"                 "0.172951385719202"

$Results$output1$value$Values[[55]]
 [1] "4"                 "3"                 "aug"               "thu"               "91.7"              "114.3"            
 [7] "661.3"             "6.3"               "17.6"              "45"                "3.6"               "0"                
[13] "0"                 "0"                 "0.231841881456964"

$Results$output1$value$Values[[56]]
 [1] "4"                 "3"                 "sep"               "thu"               "92.9"              "137"              
 [7] "706.4"             "9.2"               "27.7"              "24"                "2.2"               "0"                
[13] "0"                 "0"                 "0.128064040724407"

$Results$output1$value$Values[[57]]
 [1] "4"                 "3"                 "sep"               "tue"               "90.3"              "80.7"             
 [7] "730.2"             "6.3"               "17.8"              "63"                "4.9"               "0"                
[13] "0"                 "0"                 "0.261764834101004"

$Results$output1$value$Values[[58]]
 [1] "4"                 "3"                 "oct"               "sun"               "92.6"              "46.5"             
 [7] "691.8"             "8.8"               "13.8"              "50"                "2.7"               "0"                
[13] "0"                 "0"                 "0.252806062058886"

$Results$output1$value$Values[[59]]
 [1] "2"                   "2"                   "feb"                 "mon"                 "84"                 
 [6] "9.3"                 "34"                  "2.1"                 "13.9"                "40"                 
[11] "5.4"                 "0"                   "0"                   "0"                   "0.00884898833910594"

$Results$output1$value$Values[[60]]
 [1] "2"                  "2"                  "feb"                "fri"                "86.6"               "13.2"              
 [7] "43"                 "5.3"                "12.3"               "51"                 "0.9"                "0"                 
[13] "0"                  "0"                  "0.0424222666701839"

任何人都知道如何解决这个问题?

编辑:我已经想到了它,但如下所示,results_json_nice格式正确,但不是resultStored,我无法解决这个问题。

library(jsonlite)

results <- '{"Results":{"output1":{"type":["DataTable"],"value":{"ColumnNames":["X","Y","month","day","FFMC","DMC","DC","ISI","temp","RH","wind","rain","area","classes","probabilities"],"ColumnTypes":["Numeric","Numeric","String","String","Numeric","Numeric","Numeric","Numeric","Numeric","Numeric","Numeric","Numeric","Numeric","Categorical","Numeric"],"Values":[["0","0","value","value","0","0","0","0","0","0","0","0","0","0","0"],["0","0","value","value","0","0","0","0","0","0","0","0","0","0","0"]]}}}}'

results_df <- fromJSON(results)

results_json_nice <- toJSON(results_df, pretty = TRUE)


resultStored = ""
for(i in 1:(length(10))) { 
  resultStored = paste(resultStored,results_json_nice,sep='\n') 
}

1 个答案:

答案 0 :(得分:0)

使用库jsonlite我可以得到与您想要的最终结果相似的结果:

library(jsonlite)

results <- '{"Results":{"output1":{"type":["DataTable"],"value":{"ColumnNames":["X","Y","month","day","FFMC","DMC","DC","ISI","temp","RH","wind","rain","area","classes","probabilities"],"ColumnTypes":["Numeric","Numeric","String","String","Numeric","Numeric","Numeric","Numeric","Numeric","Numeric","Numeric","Numeric","Numeric","Categorical","Numeric"],"Values":[["0","0","value","value","0","0","0","0","0","0","0","0","0","0","0"],["0","0","value","value","0","0","0","0","0","0","0","0","0","0","0"]]}}}}'

results_df <- fromJSON(results)

results_json_nice <- toJSON(results_df, pretty = TRUE)

> cat(results_json_nice)
{
  "Results": {
    "output1": {
      "type": ["DataTable"],
      "value": {
        "ColumnNames": ["X", "Y", "month", "day", "FFMC", "DMC", "DC", "ISI", "temp", "RH", "wind", "rain", "area", "classes", "probabilities"],
        "ColumnTypes": ["Numeric", "Numeric", "String", "String", "Numeric", "Numeric", "Numeric", "Numeric", "Numeric", "Numeric", "Numeric", "Numeric", "Numeric", "Categorical", "Numeric"],
        "Values": [
          ["0", "0", "value", "value", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"],
          ["0", "0", "value", "value", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"]
        ]
      }
    }
  }
}