简体   繁体   English

通过“ {}”解析字符串

[英]Parsing string by '{ }'

The format of the output is [{uuid, Readings}, {uuid, Readings}, {uuid,Readings}] 输出的格式为[{uuid, Readings}, {uuid, Readings}, {uuid,Readings}]
(Usually longer than this) (通常比这更长)

For example ... (All one string, just formatted to be easier on the eyes). 例如...(所有一个字符串,仅格式化为在眼睛上更容易使用)。

[{"uuid": "e2e38e4e-431d-508a-a5f9-e50eaffe6aab", "Readings": [[1351329301000.0, 0.0], [1351330202000.0, 0.0], [1351331102000.0, 0.0]]}, 

{"uuid": "8ed157d9-b31d-5075-bad6-69dfde7a34a4", "Readings": [[1351329301000.0, 0.0], [1351330202000.0, 0.0], [1351411200000.0, 494.0]]}, 

{"uuid": "8737f287-14df-5ccd-82df-e66ca7374b63", "Readings": [[1351329300000.0, 515.0], [1351330200000.0, 467.0], ]}]

I want to parse this so that I get everything in between the '{ }' symbols including the '{ }' symbols. 我想对此进行解析,以使所有内容都在'{}'符号之间,包括'{}'符号之间。

I don't want to separate anything else. 我不想分开其他任何东西。 Everything in between '{ }' symbols should stay together as one string. “ {}”符号之间的所有内容都应作为一个字符串保持在一起。

Something that I'm also interested in is adding '[ ]' around each result at the end. 我也感兴趣的是在每个结果的末尾添加'[]'。

Simple example: 简单的例子:

[ {"uuid": 1, "Readings": [[2,3], [2,6]]}, {"uuid": 1, "Readings": [[5,6], [7,7]]} ]

would result in an array where 会导致一个数组,其中

element 0 = "[{"uuid": 1, "Readings": [[2,3], [2,6]]}]"
element 1 = "[{"uuid": 1, "Readings": [[5,6], [7,7]]}]"

Anyone know the regex expression to select something like this? 任何人都知道正则表达式来选择这样的东西吗? (I'm using ruby on rails btw) (我正在使用Ruby on Rails顺便说一句)

This is fairly "hacky", but I'd suggest parsing it using a JSON Parser (you'll have to see how exactly to do that yourself, I'm sure there's a library or something). 这是相当“ hacky”的,但我建议使用JSON解析器对其进行解析(您必须亲自了解如何做到这一点,我确定有一个库或其他东西)。 Then, you can stringify each object in the returned array and put them in an array of strings. 然后,您可以对返回的数组中的每个对象进行字符串化处理,并将它们放入字符串数组中。

Here's how I'd do it in javascript (you can adapt the workflow to RoR) 这是我在javascript中的操作方法(您可以使工作流程适应RoR)

objArray = JSON.parse(jsonString);
stringArray=[];
for (i=0; i<objArray.length; i++) {
    stringArray.push("[" + JSON.stringify(objArray[i]) + "]");
}

EDIT WHY you want to do it so baffles me, because having the objects in an array would be sooo much more useful 编辑为什么要这么做让我感到困惑,因为将对象放在数组中会非常有用

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

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