解析非标准JSON

发布于 2024-12-23 09:02:36 字数 652 浏览 1 评论 0原文

有人知道下面的代码是什么类型的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

合久必婚 2024-12-30 09:02:36

您可以使用 Json.Net 来解析您的输入字符串。您甚至可以在此扩展类的帮助下使用dynamic(已测试)用你的字符串)

dynamic obj = JsonUtils.JsonObject.GetDynamicJsonObject(jsonstr);
Console.WriteLine(obj.names.en);
Console.WriteLine(obj.status);
Console.WriteLine(obj.start_time_xls.en);
Console.WriteLine(obj.suspend_at);

用纯 Json.Net

JObject jObj =  (JObject)JsonConvert.DeserializeObject(json3);
Console.WriteLine(jObj["names"]["en"]);
Console.WriteLine(jObj["status"]);
Console.WriteLine(jObj["start_time_xls"]["en"]);
Console.WriteLine(jObj["suspend_at"]);

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)

dynamic obj = JsonUtils.JsonObject.GetDynamicJsonObject(jsonstr);
Console.WriteLine(obj.names.en);
Console.WriteLine(obj.status);
Console.WriteLine(obj.start_time_xls.en);
Console.WriteLine(obj.suspend_at);

With pure Json.Net

JObject jObj =  (JObject)JsonConvert.DeserializeObject(json3);
Console.WriteLine(jObj["names"]["en"]);
Console.WriteLine(jObj["status"]);
Console.WriteLine(jObj["start_time_xls"]["en"]);
Console.WriteLine(jObj["suspend_at"]);
逆蝶 2024-12-30 09:02:36

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

目前尚不完全清楚您要在哪里进行此转换到 JSON,但在 Javascript 中,您可以使用 window.JSON.stringify 将其转换为 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 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/

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 );
我是有多爱你 2024-12-30 09:02:36

是否有效(我投票“否”):

  • 读入字符串;
  • s {^\s*([a-z0-9_]+)\:} {"\1":} g

似乎适用于此数据集,我敢打赌它们'只是strcat向您输出输出,因此暂时可能是安全的。

Whether or not (I vote “not”) it's valid:

  • Read in the string;
  • s {^\s*([a-z0-9_]+)\:} {"\1":} g

seems to work for this data set, and I'll bet that they're just strcatting the output at you, so it's probably safe for the time being.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文