0

I am geting Javascript array as a result from webrequest which need to be converted/parsed to C# object for further handling. Any suggestion of .NET library or this need to be done by regex? Thank you?

<script>
{
item = new Array();

item [0] = new Array();
item [0][0]='value01';
item [0][1]='value02';

item [1] = new Array();
item [1][0]='value10';
item [1][1]='value11';

item [2] = new Array();
item [2][0]='value10';
item [2][1]='value11';
}
</script>

I know that the above code looks like JSON but, I have it as a part of a received html code. Therefore I think I need to parse it in traditional mannor.

4
  • 1
    A javascript array is very commonly written as something called 'json' look it up there are many parsers for .NET Commented Dec 20, 2013 at 21:20
  • You get it in a string? What does your string look like? Commented Dec 20, 2013 at 21:20
  • That's how the array is being created, but chances are that's not at all how you're receiving it. You're probably receiving it in JSON, in which case you'd need to implement a JSON parser for c# (which there are several). Commented Dec 20, 2013 at 21:20
  • 1
    Hi, I am not getting it as JSON, but from downloaded HTML. The javascript array is used to present the data in HTML table . Commented Dec 20, 2013 at 21:37

1 Answer 1

2

You could try JSON.net (http://json.codeplex.com/). You could get the nuget package within Visual Studio.

Here is an example of how to use it, taken from the official site:

string json = @"{
  'channel': {
    'title': 'James Newton-King',
    'link': 'http://james.newtonking.com',
    'description': 'James Newton-King's blog.',
    'item': [
      {
        'title': 'Json.NET 1.3 + New license + Now on CodePlex',
        'description': 'Annoucing the release of Json.NET 1.3, the MIT license and the source on CodePlex',
        'link': 'http://james.newtonking.com/projects/json-net.aspx',
        'category': [
          'Json.NET',
          'CodePlex'
        ]
      }
    ]
  }
}";

// LINQ to JSON
// ------------
JObject jObject = JObject.Parse(json);
string itemTitle2 = (string)jObject["channel"]["item"][0]["title"];
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you .... but pls read above. the script is within downloaded HTML

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.