Parsing custom JSON

Does anyone know what type of JSON (if even that!) Is the following code? I am extracting this from an HTML site. I am trying to parse it in C # with a JSON parser, but I need to make a lot of preparatory changes to format it as a "valid" JSON according to JSONLint. For example, variable names must have double quotation marks, not quotation marks 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 } 

It will always have the same format, and I would like to just parse it directly to an object in C # or java.

+2
java json javascript c # string-parsing
Dec 28 2018-11-12T00:
source share
3 answers

JSON requires all names to be in double quotes, so this is not valid JSON. This is a valid Javascript object. For questions about the JSON format, go here: http://json.org/

It's not entirely 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 ); 
+1
Dec 28 2018-11-12T00:
source share
β€” -

You can use Json.Net to parse your input string. You can even use dynamic , as shown below, with this extension class (Tested by your line)

 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"]); 
+4
Dec 28 '11 at 23:11
source share

Regardless of (I vote β€œnot”) it really is:

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

seems to work for this dataset, and I bet they just strcat ting the output to you, so it is probably safe at the moment.

+1
Dec 28 '11 at 23:11
source share



All Articles