Return Json object with Duplicate Keys using C#

I'm using WEB API to receive the request from the Client application to save the Contact Information and I need to send the Error Message only if data has an error otherwise nothing TODO

Early I Used Dictionary

For Example:

Dictionary<string, string> error = new Dictionary<string, string>
{
    {"SaveContactMethod_1", "FirstName Invalid"},
    {"SaveContactMethod_2", "LastName Invalid"},
    {"SaveContactMethod_3", "MiddleName Invalid"},
}

the respective JSON Object is

{
    "error" : {
        "SaveContactMethod_1":"FirstName Invalid",
        "SaveContactMethod_2":"LastName Invalid",
        "SaveContactMethod_3":"MiddleName Invalid"
    }
}

But I need an UNIQUE Key (ie, Duplicate Key), So I changed the Dictionary<string, string> to List<KeyValuePair<string, string>>

List<KeyValuePair<string, string>> error = new List<KeyValuePair<string, string>>
{
    new KeyValuePair<string, string>("SaveContactMethod", "FirstName Invalid"),
    new KeyValuePair<string, string>("SaveContactMethod", "LastName Invalid"),
    new KeyValuePair<string, string>("SaveContactMethod", "MiddleName Invalid"),
}

the respective JSON Object is

{
    "error" : [
        { "key":"SaveContactMethod", "value":"FirstName Invalid" },
        { "key":"SaveContactMethod", "value":"LastName Invalid" },
        { "key":"SaveContactMethod", "value":"MiddleName Invalid" }
    ]
}

My Requirement : I need to add a Duplicate Key and I need the Json Output as like Dictionary. Kindly assist me.

Expected Output: JSON

{
    "error" : {
        "SaveContactMethod":"FirstName Invalid",
        "SaveContactMethod":"LastName Invalid",
        "SaveContactMethod":"MiddleName Invalid"
    }
}

The said JSON is a Valid format:

Online Tool #1 : https://jsonformatter.curiousconcept.com/

有效的JSON对象

Online Tool #2 : http://jsonformatter.org/

在这里输入图像描述

Online Tool #3 : http://www.freeformatter.com/json-validator.html

在这里输入图像描述

Note : This JSON is Just for reading purpose, I need to identity the information, form where its coming. So, I used the Method name as KEY. No parser is going to read this JSON. So, no issue.


No , this is not possible.

This would be invalid* JSON:

{
    "error" : {
        "SaveContactMethod":"FirstName Invalid",
        "SaveContactMethod":"LastName Invalid",
        "SaveContactMethod":"MiddleName Invalid"
    }
}

You can check this here:

Warning:Duplicate key, names should be unique.[Code 23, Structure 9]
Warning:Duplicate key, names should be unique.[Code 23, Structure 13]

*Depending on what you call valid

If you realy want to go this route, according to RFC 4627, you could use the StringBuilder class.


Since you don't seem to understand, what Depending on what you call valid means.

ECMA-262:

In the case where there are duplicate name Strings within an object, lexically preceding values for the same key shall be overwritten.

That means: If you get three SaveContactMethod 's, you only want "MiddleName Invalid" in ECMA Script (JS). With c# serialization, this would not even be possible. You need to write your own JsonSerializer for it.


You have a classic "XY" problem. You have asked "How do I do X", but you really need to do Y and you think that X is the only way to get to Y -- but X is either impossible or very hard. By changing your requirements a little, you can get to Y a different way, but you haven't seen that yet since you're stuck on X.

Here's your X: the JSON format that you want to get:

{
    "error" : {
        "SaveContactMethod":"FirstName Invalid",
        "SaveContactMethod":"LastName Invalid",
        "SaveContactMethod":"MiddleName Invalid"
    }
}

This will, as others have said, throw away all the error messages except for one when you load it into your C# code.

However, there's a very simple way to get all the error messages. You simply need to change the JSON you're expecting to look something like this instead:

{
    "error" : {
        "SaveContactMethod": [
            "FirstName Invalid",
            "LastName Invalid",
            "MiddleName Invalid"
        ]
    }
}

If you only had a single error message, you should still use a list:

{
    "error" : {
        "SaveContactMethod": [
            "FirstName Invalid"
        ]
    }
}

That way when you load the JSON into your C# code, it will always have the same type, Dictionary<string,List<string>> , whether there was one error or many.

That's the Y in your XY problem. Instead of beating your head against the wall of "I want to have duplicate keys in JSON", find a way around the wall: have a single key with a list of values. And now you can do what you really needed, which is to get all the error messages from your form with just a single key name for every single error message.

链接地址: http://www.djcxy.com/p/37828.html

上一篇: 合并连接查询中的JSON(B)列

下一篇: 使用C#返回带有重复键的Json对象