解析非标准JSON [英] Parsing non-standard JSON

查看:178
本文介绍了解析非标准JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都知道以下代码是什么类型的JSON(如果是这样!)我正在从网站的HTML中检索这个。我正在尝试使用JSON解析器在C#中解析它,但我必须进行大量的预备编辑,根据JSONLint将其格式化为有效JSON。例如,变量的名称都应该有双引号而不是没有引号。

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
}

这将始终具有相同的格式,我很乐意直接将其解析为C#或java中的对象。

This will always have the same format and I'd love to just parse it straight to an object in C# or java.

推荐答案

JSON要求所有名称都是双引号,因此这不是有效的JSON。这是一个有效的Javascript对象。有关JSON格式的问题,请访问: http://json.org/

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/

目前还不完全清楚您希望将此转换为JSON,但在Javascript中,您可以使用 window.JSON.stringify 将其转换为JSON。

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.

演示: http://jsfiddle.net/ThinkingStiff/3xZD8 /

var object = {
    names: {
        en: 'VirtualMarket-2MinuteLevel',
        es: 'VirtualMarket-2MinuteLevel'
    },
    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
    },
    json = window.JSON.stringify( object );

这篇关于解析非标准JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆