0

I have db.js file witch cotain 2 type of javascrip objec : question , answer

var questions = [
    { id: 1, src: "images/findbugQ/P1.jpg", dif: 1, type: "findbugs", description: "" },
    { id: 2, src: "images/findbugQ/P2.jpg", dif: 1, type: "findbugs", description: "" }]


var answer= [
    { id: 1, questionId: 1, width: "12%", height: "6%", top: "7%", left: "65%" },
    { id: 2, questionId: 2, width: "7%", height: "6%", top: "8%", left: "11%" },
    { id: 3, questionId: 3, width: "5%", height: "6%", top: "6%", left: "50%" }]

how can I get question[1].id in C# code whith URL of db.js file is /js/db.js

duhdh12
  • 33
  • 1
  • 10
  • Where is the data coming from originally? Is it hard-coded? – mellamokb Apr 17 '14 at 16:08
  • You'd have to parse the script file and extract the values from the resulting state. And, .NET doesn't have such a parser built-in. "[Parse JavaScript code in C#](http://stackoverflow.com/questions/720678/parse-javascript-code-in-c-sharp)" may help with that. Though, if you have control over `db.js`, you could rewrite it as a JSON file. – Jonathan Lonowski Apr 17 '14 at 16:13

3 Answers3

0

Simple answer. You can't

There are some limits for server side coding and client side coding.

What you can do is send a JSON string to some generic handler and parse it from there.

Amit Joki
  • 58,320
  • 7
  • 77
  • 95
0

take a look at the following question. you can communicate with these engines and get your answers that way:embedding javascript engine into .net

Community
  • 1
  • 1
Nathan Tregillus
  • 6,006
  • 3
  • 52
  • 91
0

You can not load JS file in C# in the format you want.

But you can do following way.

  • Instead of simple two array, use java script object which you can deserialize using JavaScriptSerilizer

If I have to do this I can do following way.

  1. Create C# class with all property same name as I required in JS.
  2. I fill those object and serialize this object using JavaScriptSerilizer so It give me one JSON format.

Next time whatever data I have I fill in JSON format so I can deserilize in C#.

dotnetstep
  • 17,065
  • 5
  • 54
  • 72