0

I need to read a javascript array from a .js file. The .js file has comments "//..." as well. Here's what the file looks like :

var myarraydata = [ 
{
    col1: "value1", 
    col2: "value2", 
    col3: "value3"
},
{
    col1: "x1", 
    col2: "x2", 
    col3: "x3"
}];

In the C# windows application. A DataTable? with

columns : col1    col2    col3
Values  : value1  value2  value3
          x1      x2      x3

....Something like

File.ReadAllText(....
//Parse the data
3
  • what have you try to do? Commented Nov 20, 2015 at 13:24
  • @Liam JSON and JavaScript are not the same thing. That is not a valid JSON file. Commented Nov 20, 2015 at 14:21
  • Yup, its a js array :) being loaded via a .js file Commented Nov 20, 2015 at 14:34

2 Answers 2

4

JavaScript arrays are representable with JSON objects. So you may could serialize and deserialize them using Newtonsoft.Json. Please correct me if i am wrong.

Just take a look at: Json.NET

EDIT

Newtonsoft.Json is release under the MIT license. So you should be able to embed the source code directly, if needed. But in your case i would use the assembly, because updating to a newer version will be much easier.

Sign up to request clarification or add additional context in comments.

3 Comments

thank you. Any points at not having to do this without including additional libraries?
Write your own JSON serialization. But you might download the sources of it and embed it into yours, if the licenses allows this. But the library is very lightweight and excellent to use.
Tx. I think I'll do some string parsing directly in c# and split the values using a basic regex. May be clunky but should avoid a bit of code. Thought someone may have a better approach than my initial thoughts. Some model view scenario etc...:)
0

If anyones interested, I got this working using the standard .net classes :)

Microsoft Reference

Here's sample code from the website :

using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.Script.Serialization;

namespace ExampleApplication
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var RegisteredUsers = new List<Person>();
            RegisteredUsers.Add(new Person() { PersonID = 1, Name = "Bryon Hetrick", Registered = true });
            RegisteredUsers.Add(new Person() { PersonID = 2, Name = "Nicole Wilcox", Registered = true });
            RegisteredUsers.Add(new Person() { PersonID = 3, Name = "Adrian Martinson", Registered = false });
            RegisteredUsers.Add(new Person() { PersonID = 4, Name = "Nora Osborn", Registered = false });

            var serializer = new JavaScriptSerializer();
            var serializedResult = serializer.Serialize(RegisteredUsers);
            // Produces string value of:
            // [
            //     {"PersonID":1,"Name":"Bryon Hetrick","Registered":true},
            //     {"PersonID":2,"Name":"Nicole Wilcox","Registered":true},
            //     {"PersonID":3,"Name":"Adrian Martinson","Registered":false},
            //     {"PersonID":4,"Name":"Nora Osborn","Registered":false}
            // ]

            var deserializedResult = serializer.Deserialize<List<Person>>(serializedResult);
            // Produces List with 4 Person objects
        }
    }
}

Thank you MS

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.