Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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>
share|improve this question
1  
A javascript array is very commonly written as something called 'json' look it up there are many parsers for .NET –  Kevin DiTraglia 32 mins ago
 
You get it in a string? What does your string look like? –  bobek 32 mins ago
 
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). –  r3mus 32 mins ago
 
possible duplicate of Parsing json array using json.net –  allonhadaya 31 mins ago
 
Hi, I am not getting it as JSON, but from downloaded HTML. The javascript array is used to present the data in HTML table . –  Jim 16 mins ago
add comment

1 Answer

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"];
share|improve this answer
 
Thank you .... but pls read above. the script is within downloaded HTML –  Jim 14 mins ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.