在json中得到错误 [英] getting error in json
问题描述
使用System;
使用System.Data;
使用System.Configuration;
使用System.Collections;
使用System.Web;
使用System.Web.Security;
使用System.Web.UI;
使用System.Web .UI.WebControls;
使用System.Web.UI.WebControls.WebParts;
使用System.Web.UI.HtmlControls;
using Newtonsoft.Json;
使用System.Collections.Generic;
命名空间JSONFromCS
{
public partial class _Default:System.Web.UI.Page
{
protected void Page_Load(object sender,EventArgs e1)
{
列表< 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 +'
年龄:='+ employeeList.Employee [i] .Age);;
script + =};
ClientScriptManager cs = Page.C lientScript;
cs.RegisterStartupScript(Page.GetType(),JSON,脚本,true);
}
}
公共班级员工
{
公共字符串名称;
public int年龄;
}
}
错误是
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
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
推荐答案
所有你需要的是更准确地写字符串,并提出问题。使用HTML元素pre使用正确的格式:
All you need is to be more accurate in writing strings, and asking questions, too. Use proper formatting using the HTML element "pre":
<pre lang="C#">
… your code here …
</pre>
另请参阅我对该问题的评论。
这是我的建议:停止使用这些字符串连接(很少是个好主意),使用方法的String.format
。请自行修改您的代码;这太微不足道了。你需要让自己能够流利地使用这些东西。
祝你好运。
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.
这篇关于在json中得到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!