解析非标准JSON
有人知道下面的代码是什么类型的 JSON(如果是的话!)?我正在从网站的 HTML 中检索此内容。我正在尝试使用 JSON 解析器在 C# 中解析它,但我必须进行大量的准备编辑才能根据 JSONLint 将其格式化为“有效”JSON。例如,变量的名称都应该有双引号,而不是根本没有引号。
{
status: 'A',
displayed: 'Y',
start_time: '2010-11-2600: 00: 00',
start_time_xls: {
en: '26thofNov201000: 00am',
es: '26Nov201000: 00am'
},
suspend_at: '2010-11-2619: 57: 59',
is_off: 'Y',
score_home: '',
score_away: '',
bids_status: '',
period_id: '',
curr_period_start_time: '',
score_extra_info: '',
ev_id: 2257335,
blurb: '',
last_mkts_of_day: false,
follow_hcap_mkt: 10999896
}
这将始终具有相同的格式,我希望将其直接解析为 C# 或 java 中的对象。
Anyone know what type of JSON (if even that!) the following code is? I'm retrieving this from the HTML of a website. I'm trying to parse it in C# with a JSON parser, but I'm having to do lots of preparatory editing to format it as 'valid' JSON according to JSONLint. For example, the names of the variables should all have double quotes rather than having no quotes at all.
{
status: 'A',
displayed: 'Y',
start_time: '2010-11-2600: 00: 00',
start_time_xls: {
en: '26thofNov201000: 00am',
es: '26Nov201000: 00am'
},
suspend_at: '2010-11-2619: 57: 59',
is_off: 'Y',
score_home: '',
score_away: '',
bids_status: '',
period_id: '',
curr_period_start_time: '',
score_extra_info: '',
ev_id: 2257335,
blurb: '',
last_mkts_of_day: false,
follow_hcap_mkt: 10999896
}
This will always have the same format and I'd love to just parse it straight to an object in C# or java.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 Json.Net 来解析您的输入字符串。您甚至可以在此扩展类的帮助下使用
dynamic
(已测试)用你的字符串)用纯 Json.Net
You can use Json.Net to parse your input string. You can even make use of
dynamic
as below with the help of this extension class (Tested with your string)With pure Json.Net
JSON 要求所有名称都用双引号引起来,因此这不是有效的 JSON。这是一个有效的 Javascript 对象。对于 JSON 格式问题,请转到此处:http://json.org/
目前尚不完全清楚您要在哪里进行此转换到 JSON,但在 Javascript 中,您可以使用 window.JSON.stringify 将其转换为 JSON。
演示:http://jsfiddle.net/ThinkingStiff/3xZD8/
JSON requires that all names be in double quotes, so this is not valid JSON. This is a valid Javascript object. For JSON format questions go here: http://json.org/
It's not totally clear where you want to do this conversion to JSON, but in Javascript you can use
window.JSON.stringify
to convert it to JSON.Demo: http://jsfiddle.net/ThinkingStiff/3xZD8/
是否有效(我投票“否”):
s {^\s*([a-z0-9_]+)\:} {"\1":} g
似乎适用于此数据集,我敢打赌它们'只是
strcat
向您输出输出,因此暂时可能是安全的。Whether or not (I vote “not”) it's valid:
s {^\s*([a-z0-9_]+)\:} {"\1":} g
seems to work for this data set, and I'll bet that they're just
strcat
ting the output at you, so it's probably safe for the time being.