简体   繁体   English

将json数组中的单个项目发布到.net对象

[英]posting single item in json array to .net object

I have an object in .net that is mostly for the purpose of receiving some json. 我在.net中有一个对象,主要是为了接收一些json。 It generally works very nicely, but when there is a single item in an array of numbers the json library converts this to a single number and not an array with a single item. 它通常工作得非常好,但是当数字数组中有一个项目时,json库会将其转换为单个数字而不是具有单个项目的数组。 .net throws this out as an error as it is a single int32 instead of an array of int32s i tried converting to json.net but this had not surprisingly the same error .net抛出这个作为一个错误,因为它是一个单一的int32而不是我尝试转换为json.net的int32s数组但是这并没有出乎意料的错误

as far as i can think there is no way to have an alternative definition in my object, is there? 据我所知,没有办法在我的对象中有另一种定义,是吗?

below is the definition of my object 下面是我的对象的定义

public class EnquiryModel
{
    public string Name;
    public string Email;
    public string Phone;
    public string JobCode;
    public string Message;
    public int ReferralSource;
    public Dictionary<string, int> PickList;
    public int[] Studios;
    public int[] Services;
    public string[] BookingEnquiries;
}

and here is the code i use to populate it 这是我用来填充它的代码

using Newtonsoft.Json;
EnquiryModel enq = JsonConvert.DeserializeObject<EnquiryModel>(json);

previously I used 以前我用过

using System.Web.Script.Serialization;
JavaScriptSerializer j = new JavaScriptSerializer();
EnquiryModel enq = j.Deserialize<EnquiryModel>(json);

they both produce the same error 他们都产生同样的错误

not sure what the best way to avoid this problem would be 不确定避免这个问题的最佳方法是什么

when it is serialised on the client the arrays with a single item are converted to a single number not sure why that is either :) ? 当它在客户端上序列化时,带有单个项目的数组将转换为单个数字,不确定为什么要么:)?

UPDATED AS SOLVED 更新已解决

the answer marked as the solution below worked really well - thank you :) 标记为下面的解决方案的答案工作得非常好 - 谢谢:)

there was a couple of tiny changes I had to make which I thought were worth sharing 我必须做出一些微小的改变,我认为值得分享

firstly I found it was passed as an Int64 so my check is for both types with an or 首先我发现它是作为Int64传递的,所以我的检查是针对两种类型的一个或

also I had some code that used this object and because the public variable was no longer an array but an object I had to add a cast to the usage so: 我也有一些使用这个对象的代码,因为公共变量不再是一个数组,而是一个对象,我不得不在使用中添加一个强制转换:

foreach (int studio in enq.Studios)

had to change to 不得不改为

foreach (int studio in (Int32[])enq.Studios)

this is the full source of the object now it would be good if there were some way to generalise the repeated code to make it easier to read but that may well be gold-plating :) 这是对象的完整源代码现在如果有一些方法来概括重复的代码以使其更容易阅读但是很可能是镀金的话会很好:)

public class EnquiryModel
{
    public string Name;
    public string Email;
    public string Phone;
    public string JobCode;
    public string Message;
    public int ReferralSource;
    public Dictionary<string, int> PickList;
    //allow the arrays of 1 to many values to be submitted as a single value
    // instead of a single item in an array
    private string[] bookingEnquiries;
    public object BookingEnquiries
    {
        get { return bookingEnquiries; }
        set
        {
            if (value.GetType() == typeof(string))
            {
                bookingEnquiries = new string[] { (string)value };
            }
            else if (value.GetType() == typeof(string[]))
            {
                bookingEnquiries = (string[])value;
            }
        }
    }
    private int[] studios;
    public object Studios
    {
        get { return studios; }
        set
        {
            if (value.GetType() == typeof(Int32) || value.GetType() == typeof(Int64))
            {
                studios = new Int32[] { (Int32)value };
            }
            else if (value.GetType() == typeof(Int32[]))
            {
                studios = (Int32[])value;
            }
        }
    }
    private int[] services;
    public object Services
    {
        get { return services; }
        set
        {
            if (value.GetType() == typeof(Int32) || value.GetType() == typeof(Int64))
            {
                services = new Int32[] { (Int32)value };
            }
            else if (value.GetType() == typeof(Int32[]))
            {
                services = (Int32[])value;
            }
        }
    }

}

I would make the variables p[rivate and expose them through getters and setters. 我会使变量p [rivate并通过getter和setter公开它们。 In the setter, you can evaluate the property sent in and set it appropriately. 在setter中,您可以评估发送的属性并进行适当的设置。

    private Int32[] numbers;

    public object Numbers
    {
        get { return numbers; }
        set
        {
            if (value.GetType() == typeof(Int32))
            {
                numbers = new Int32[] { (Int32)value };
            }
            else if (value.GetType() == typeof(Int32[]))
            {
                numbers = (Int32[])value;
            }
        }
    }

I feel you will need to write your own wrapper which will check that if a single item of type T exists, it converts it to new T[]{item} . 我觉得你需要编写自己的包装器,它将检查如果存在T类型的单个项目,它会将其转换为new T[]{item} You can use JSON.NET's JObject class. 您可以使用JSON.NET的JObject类。

JObject o = JObject.Parse(response);

and evaluate o for the particular property you are looking for. 并评估o你正在寻找的特定属性。

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

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