Click here to Skip to main content
15,886,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace JSONFromCS
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e1)
{
List<employee> eList = new List<employee>();
Employee e = new Employee();
e.Name = "Minal";
e.Age = 24;

eList.Add(e);

e = new Employee();
e.Name = "Santosh";
e.Age = 24;

eList.Add(e);

string ans = JsonConvert.SerializeObject(eList, Formatting.Indented);

string script = "var employeeList = {\"Employee\": " + ans+"};";
script += "for(i = 0;i<employeelist.employee.length;i++)";>
script += "{";
script += "alert ('Name : ='+employeeList.Employee[i].Name+'
Age : = '+employeeList.Employee[i].Age);";
script += "}";

ClientScriptManager cs = Page.ClientScript;
cs.RegisterStartupScript(Page.GetType(), "JSON", script, true);
}
}
public class Employee
{
public string Name;
public int Age;
}
}


error is


CSS
Error   4   Newline in constant E:\New folder\homepage\Default.aspx.cs  38  23  homepage
Error   6   Newline in constant E:\New folder\homepage\Default.aspx.cs  39  21  homepage
Error   5   Too many characters in character literal    E:\New folder\homepage\Default.aspx.cs  39  21  homepage
Posted
Comments
Sergey Alexandrovich Kryukov 22-Sep-14 0:51am    
In what lines? Please, no one likes to count your lines. Please comment each error on the respective line.
And what's the problem, aren't those error messages clear enough?
—SA
Nandakishore G N 22-Sep-14 1:35am    
No need of concatenation in server side . Its better you can use ajax calls and receive your list to design side.
Refer this link, It may help you
http://www.codeproject.com/Tips/629042/Example-Of-JavaScript-Serializer-And-Json-String-U

1 solution

All you need is to be more accurate in writing strings, and asking questions, too. Use proper formatting using the HTML element "pre":
HTML
<pre lang="C#">
   … your code here …
</pre>
See also my comment to the question.

Here is my advice: stop using these string concatenation (rarely a good idea), use the method string.Format. Please fix your code by yourself; this is way too trivial. You need to make yourself fluent in such things.

Good luck.
—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900