我在一个json文件中有相同格式的大量json对象 如下。我想将它们转换为R数据帧,然后提取lantency的所有值。但是当我输入命令时
json_data <- fromJSON(file=json_flie)
只有第一个json对象存储在数据帧中,所以该怎么办? 谢谢!
{&#34;任务&#34;:[{&#34;类型&#34;:&#34;平&#34;&#34; ID&#34;:1,&#34;值&# 34;:&#34; 159 159 152 153 149 147 150 151 148 149&#34;,&#34; IsFinished&#34;:true},{&#34; type&#34;:&#34; latency&#34;,&#34; id& #34;:2&#34;值&#34; 147&#34; IsFinished&#34;:真},{&#34;类型&#34;:&#34;可以通过&#34;&# 34; ID&#34;:3,&#34;值&#34;:&#34;&#34;&#34; IsFinished&#34;:假},{&#34;类型&#34;:& #34; DNS&#34;&#34; ID&#34:4,&#34;值&#34;:12,&#34; IsFinished&#34;:真}],&#34; measurementTimes&#34 ;:10,&#34; URL&#34;:&#34;&#34;} {&#34;任务&#34;:[{&#34;类型&#34;:&#34;平&#34 ;,&#34; ID&#34;:1,&#34;值&#34;:&#34; 166 165 179 181 159 162 166 159 161 162&#34;,&#34; IsFinished&#34;:true},{&#34; type&#34;:&#34; latency&#34;,&#34; id& #34;:2&#34;值&#34;:159,&#34; IsFinished&#34;:真},{&#34;类型&#34;:&#34;可以通过&#34;&# 34; ID&#34;:3,&#34;值&#34;:&#34;&#34;&#34; IsFinished&#34;:假},{&#34;类型&#34;:& #34; DNS&#34;&#34; ID&#34:4,&#34;值&#34;:7,&#34; IsFinished&#34;:真}],&#34; measurementTimes&#34 ;:10,&#34; URL&#34;:&#34;&#34;} {&#34;任务&#34;:[{&#34;类型&#34;:&#34;平&#34 ;,&#34; ID&#34;:1,&#34;值&#34;:&#34; 172 172 159 160 159 159 159 158 160 162&#34;,&#34; IsFinished&#34;:true},{&#34; type&#34;:&#34; latency&#34;,&#34; id& #34;:2&#34;值&#34;:158,&#34; IsFinished&#34;:真},{&#34;类型&#34;:&#34;可以通过&#34;&# 34; ID&#34;:3,&#34;值&#34;:&#34;&#34;&#34; IsFinished&#34;:假},{&#34;类型&#34;:& #34; DNS&#34;&#34; ID&#34:4,&#34;值&#34;:14,&#34; IsFinished&#34;:真}],&#34; measurementTimes&#34 ;:10,&#34; URL&#34;:&#34;&#34;} {&#34;任务&#34;:[{&#34;类型&#34;:&#34;平&#34 ;,&#34; ID&#34;:1,&#34;值&#34;:&#34; 182 192 171 184 160 159 156 157 180 171&#34;,&#34; IsFinished&#34;:true},{&#34; type&#34;:&#34; latency&#34;,&#34; id& #34;:2&#34;值&#34; 156&#34; IsFinished&#34;:真},{&#34;类型&#34;:&#34;可以通过&#34;&# 34; ID&#34;:3,&#34;值&#34;:&#34;&#34;&#34; IsFinished&#34;:假},{&#34;类型&#34;:& #34; DNS&#34;&#34; ID&#34:4,&#34;值&#34;:26,&#34; IsFinished&#34;:真}],&#34; measurementTimes&#34 ;:10,&#34; URL&#34;:&#34;&#34;} {&#34;任务&#34;:[{&#34;类型&#34;:&#34;平&#34 ;,&#34; ID&#34;:1,&#34;值&#34;:&#34; 158 186 168 189 190 233 168 160 188 157&#34;,&#34; IsFinished&#34;:true},{&#34; type&#34;:&#34; latency&#34;,&#34; id& #34;:2&#34;值&#34; 157&#34; IsFinished&#34;:真},{&#34;类型&#34;:&#34;可以通过&#34;&# 34; ID&#34;:3,&#34;值&#34;:&#34;&#34;&#34; IsFinished&#34;:假},{&#34;类型&#34;:& #34; DNS&#34;&#34; ID&#34:4,&#34;值&#34;:1,&#34; IsFinished&#34;:真}],&#34; measurementTimes&#34 ;:10,&#34; URL&#34;:&#34;&#34;}
答案 0 :(得分:3)
您的输入JSON格式错误,并且在根级别具有多个元素"task"
。这类似于定义具有多个根的XML文档,这当然是不允许的。如果您创建一个包含"task"
元素数组的外部元素,那么您将能够使用fromJSON
将文件成功加载到R中。这是文件应该是什么样子:
{
"root" : [
{
"task":
[
{"type":"ping","id":1,"value":" 159 159 152 153 149 147 150 151 148 149","IsFinished":true},
{"type":"latency","id":2,"value":147,"IsFinished":true},
{"type":"throughput","id":3,"value":"","IsFinished":false},
{"type":"DNS","id":4,"value":12,"IsFinished":true}
],
"measurementTimes":10,
"url":""
},
{
"task":
[
{"type":"ping","id":1,"value":" 166 165 179 181 159 162 166 159 161 162","IsFinished":true},
{"type":"latency","id":2,"value":159,"IsFinished":true},
{"type":"throughput","id":3,"value":"","IsFinished":false},\
{"type":"DNS","id":4,"value":7,"IsFinished":true}
],
"measurementTimes":10,
"url":""
},
... and so on for other entries
]
}
以下是我在R控制台中看到的内容:
> summary(json_data)
Length Class Mode
root 5 -none- list
输入变量名json_data
给了我整个JSON结构的转储。