简体   繁体   English

使用Jquery / Javascript读取NewtonSoft Json数据

[英]Reading NewtonSoft Json Data with Jquery/Javascript

I am facing problem while using the object that i have sent from my controller to view using Json. 我在使用从控制器发送来使用Json查看的对象时遇到问题。

I am sending List of Objects to the View by using NewtonSoft.Json 7.xx to serialize the List to Json. 我正在使用NewtonSoft.Json 7.xx将对象列表发送到视图,以将列表序列化为Json。 I am using the below code to serialize: 我正在使用以下代码进行序列化:

return JsonConvert.SerializeObject(DataToSend, Formatting.None, new JsonSerializerSettings()
        {
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore
        });

I have 2 Entity Classes: 1) Form 2) FormFields 我有2个实体类:1)表单2)FormFields

There is a 1-to-Many Relationship between these 2 entities. 这两个实体之间存在一对多关系。

public class Form
{
    public long ID { get; set; }
    public string Name { get; set; }
    public virtual List<FormField> FormFields { get; set; }
}

public class FormField
{
    public long FormID { get; set; }
    public string FieldLabel { get; set; }
    public string FieldType { get; set; }
    public string FieldValue { get; set; }
    public virtual Form form { get; set; }
}

I am trying to send the List of FormField to the view for rendering using Javascript. 我正在尝试将FormField的列表发送到视图以使用Javascript进行渲染。 I am able to send it using the above serializaton Method. 我可以使用上述序列化方法发送它。

But the problem is that, When i receive the Array of Objects in Javascript. 但是问题是,当我收到Javascript中的对象数组时。 It has Json Reference object IDs. 它具有Json参考对象ID。 I am not able to access those objects normally. 我无法正常访问这些对象。

I am able to render the 1st FormField value in that array but i am not able to render rest of them. 我能够在该数组中呈现第一个FormField值,但我无法呈现其余部分。 It is coming as Undefined. 它即将成为未定义。

I am attaching a screenshot of JSON Object Values which i am receiving on UI. 我要附上我在UI上收到的JSON对象值的屏幕截图。 You can see that there is an Object array. 您可以看到有一个对象数组。 Each Object should have the Object of Type FormField and should have that field's value but there isn't. 每个对象都应该具有FormField类型的对象,并且应该具有该字段的值,但是没有。

Only the Object at index 0 is having the values and Rest of the Indexes have only Reference IDs. 只有索引0处的对象具有值,其余索引仅具有引用ID。

Json对象值

Please help me resolve it. 请帮我解决。

Thanks 谢谢

I don't know, if it is the best solution or not but I am posting so that may be it can save someone else's struggle to find the solution. 我不知道这是否是最好的解决方案,但我正在发布,以便它可以节省其他人寻找解决方案的努力。

I Managed to do it using the .NET extension library plugin. 我设法使用.NET扩展库插件来做到这一点。 Instead of using Newtonsoft.Json , I used System.Web.Extension DLL. 而不是使用Newtonsoft.Json ,而是使用System.Web.Extension DLL。

I put the attribute ScriptIgnore in my models as: 我将属性ScriptIgnore放入模型中,如下所示:

public class Form
{
    public long ID { get; set; }
    public string Name { get; set; }

    [ScriptIgnore]
    public virtual List<FormField> FormFields { get; set; }
}

public class FormField
{
    public long FormID { get; set; }
    public string FieldLabel { get; set; }
    public string FieldType { get; set; }
    public string FieldValue { get; set; }

    [ScriptIgnore]
    public virtual Form form { get; set; }
}

Then, I serialized it with the System.Web.Script.Serialization.JavaScriptSerializer as: 然后,我使用System.Web.Script.Serialization.JavaScriptSerializer将其序列化为:

return new JavaScriptSerializer().Serialize(DataToSend);

It will serialize all the data but it will ignore the Properties which are having the attribute ScriptIgnore and it won't try to serialize them. 它将序列化所有数据,但将忽略具有ScriptIgnore属性的ScriptIgnore ,并且不会尝试对它们进行序列化。 For me it worked as required. 对我来说,它按要求工作。

Just came to know that there is an Attribute JsonIgnore provided by NewtonSoft.Json that does the same job as ScriptIgnore . 刚刚才知道有一个属性JsonIgnore提供NewtonSoft.Json ,做同样的工作作为ScriptIgnore So, If you are using NewtonSoft Library then you can also use this. 因此,如果您使用的是NewtonSoft Library,那么您也可以使用它。

It's good to know there are more ways to achieve the same result ;) 很高兴知道还有更多方法可以达到相同的结果;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM