0

This is my script which returns me a array . I have stored that array in a text field now i want to access that text field in code behind and iterate over it to get the items in this array . Is this approach is possible ? Because if i access it from text field it will be an string not an array .

Basically i want this JSON data in my code behind file .

 <script type="text/javascript">
               function sendArray() {
                   var newAry = JSON.stringify(data);
                   alert(newAry);
                   console.log(newAry);
                   document.getElementById('dataarray').value = newAry;
               }
           </script>

   <asp:Button ID="MSAVE" runat="server" Text="SAVE" Width="78px" 
           Enabled="False" CausesValidation="false" OnClientClick="sendArray();" onclick="MSAVE_Click"   />

My code behind file

protected void MSAVE_Click(object sender, EventArgs e)
    {
     String MyArrayFromJs = dataarray.Text;

}

this is what newAry look like

newAry

Grundy
  • 13,356
  • 3
  • 35
  • 55
meraname
  • 53
  • 2
  • 11
  • 1
    so just parse this string back to array – Grundy Oct 08 '15 at 06:22
  • Can you please give a description that how can i parse it back and then iterate over it to get values from key . It can't be directly parse i think because it has key and values . – meraname Oct 08 '15 at 06:26
  • just provide a bit more code: where you want use this array, how you pass it to server, what instrument you use on server? – Grundy Oct 08 '15 at 06:29
  • Now take a look at question the **String MyArrayFromJs** holds the array but in string format now i want to parse that string in array and loop through it with key and values . – meraname Oct 08 '15 at 06:35
  • 1
    Possible duplicate of [How can I parse JSON with C#?](http://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c) – Grundy Oct 08 '15 at 06:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91702/discussion-between-grundy-and-meraname). – Grundy Oct 08 '15 at 06:38
  • Just serialize the array into T type list it's the easiest way – User2012384 Oct 08 '15 at 06:53

1 Answers1

1

You have more than a simple array of strings in there from what it seems. Have a look at the String.Split method in C#. You'll want to split your data on a few different chars, probably first comma, then colon, and then clean out some rubbish characters like curly brackets etc.

However the main issue is porobably that the JSON data you show is not just an array of strings, you'll need to figure out what you really need from it.

Culme
  • 1,065
  • 13
  • 21
  • 1
    why you suggest parse it manually with splitting, when in .Net already many libs for that, include native class `JavaScriptSerializer` from .Net – Grundy Oct 08 '15 at 06:52
  • @Grundy: Because the OP asks how to transform the data into an "array"; I assumed OP needs precise control over what goes where. If the OP knows the type of object the data represents, by all means deserializing it with a built in function would be better. – Culme Oct 08 '15 at 07:01