Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
478 views
in Technique[技术] by (71.8m points)

c# - Parsing non-standard 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
}

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...