最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Parsing non-standard JSON - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited Dec 12, 2012 at 20:17 asked Dec 28, 2011 at 22:58 user971580user971580 7
  • 5 That's not JSON, it's a native JS object. – Jared Farrish Commented Dec 28, 2011 at 23:00
  • 3 Looks to me like somebody rolled their own JSON emitter and got the details wrong. – Argyle Commented Dec 28, 2011 at 23:00
  • 1 That's not HTML nor JSON. It's JavaScript (granted, in a HTML script tag). – Felix Kling Commented Dec 28, 2011 at 23:02
  • JSON-parseable string: jsfiddle/yb5Pb – Jared Farrish Commented Dec 28, 2011 at 23:03
  • 1 A decent primer: stackoverflow./questions/6489783/… – Jared Farrish Commented Dec 28, 2011 at 23:09
 |  Show 2 more ments

3 Answers 3

Reset to default 4

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"]);

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/

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/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 );

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.

发布评论

评论列表(0)

  1. 暂无评论