重新排序类中的对象

本文关键字:对象 排序 新排序 | 更新日期: 2023-09-27 18:07:24

我正在做JSON解析,我有以下结果;

{
"alias": {
    "OUTLET_NAME": "OUTLET_NAME"
},
"displayFieldName": "OUTLET_NAME",
"feature": {
    "features": [
        {
            "attribute": {
                "OUTLET_NAME": "CCK 24-hr Family Clinic"
            },
            "geometry": {
                "X": 17961.38,
                "Y": 40715.6875
            }
        },
        {
            "attribute": {
                "OUTLET_NAME": "Central Clinic and Surgery (Bedok)"
            },
            "geometry": {
                "X": 39091.43,
                "Y": 34142.043
            }
        },
        {
            "attribute": {
                "OUTLET_NAME": "Central Clinic and Surgery (Hougang)"
            },
            "geometry": {
                "X": 33835.53,
                "Y": 39431.01
            }
        },
        {
            "attribute": {
                "OUTLET_NAME": "Central Clinic and Surgery (Yishun)"
            },
            "geometry": {
                "X": 27410.125,
                "Y": 45743.45
            }
        },
        {
            "attribute": {
                "OUTLET_NAME": "Lifelink 24hrs Clinic and Surgery"
            },
            "geometry": {
                "X": 32343.502,
                "Y": 39151.2266
            }
        },
        {
            "attribute": {
                "OUTLET_NAME": "Medihealth Bishan 24 Hours Clinic and Surgery"
            },
            "geometry": {
                "X": 29984.7734,
                "Y": 36586.86
            }
        },
        {
            "attribute": {
                "OUTLET_NAME": "Mediline Wei Min Clinic"
            },
            "geometry": {
                "X": 36201.6328,
                "Y": 31674.46
            }
        },
        {
            "attribute": {
                "OUTLET_NAME": "Novena Clinic 24 Hours"
            },
            "geometry": {
                "X": 29197.53,
                "Y": 33659.6055
            }
        },
        {
            "attribute": {
                "OUTLET_NAME": "Raffles Medical Airport 24-hr Clinic"
            },
            "geometry": {
                "X": 44899.6836,
                "Y": 37431.73
            }
        },
        {
            "attribute": {
                "OUTLET_NAME": "Silver Cross Family Clinic (Serangoon)"
            },
            "geometry": {
                "X": 32329.1758,
                "Y": 37284.06
            }
        },
        {
            "attribute": {
                "OUTLET_NAME": "Shenton Family Medical Clinic (Bukit Gombak)"
            },
            "geometry": {
                "X": 18838.0039,
                "Y": 37873.7656
            }
        },
        {
            "attribute": {
                "OUTLET_NAME": "Medihealth Bishan 24 Hours Clinic and Surgery"
            },
            "geometry": {
                "X": 39090.3164,
                "Y": 34147.3477
            }
        },
        {
            "attribute": {
                "OUTLET_NAME": "Ang Mo Kio Polyclinic (NHG)"
            },
            "geometry": {
                "X": 29549.7285,
                "Y": 39377.99
            }
        },
        {
            "attribute": {
                "OUTLET_NAME": "Bedok Polyclinic (Singhealth)"
            },
            "geometry": {
                "X": 38940.74,
                "Y": 34247.7461
            }
        },
        {
            "attribute": {
                "OUTLET_NAME": "Bukit Batok Polyclinic (NHG)"
            },
            "geometry": {
                "X": 18479.2949,
                "Y": 37117.1133
            }
        },
        {
            "attribute": {
                "OUTLET_NAME": "Choa Chu Kang Polyclinic (NHG)"
            },
            "geometry": {
                "X": 18832.752,
                "Y": 40480.02
            }
        },
        {
            "attribute": {
                "OUTLET_NAME": "Clementi Polyclinic (NHG)"
            },
            "geometry": {
                "X": 20473.0254,
                "Y": 32777.65
            }
        },
        {
            "attribute": {
                "OUTLET_NAME": "Geylang Polyclinic (Singhealth)"
            },
            "geometry": {
                "X": 33995.8047,
                "Y": 33537.26
            }
        },
        {
            "attribute": {
                "OUTLET_NAME": "Hougang Polyclinic (NHG)"
            },
            "geometry": {
                "X": 34198.1172,
                "Y": 39108.6953
            }
        },
        {
            "attribute": {
                "OUTLET_NAME": "Jurong Polyclinic (NHG)"
            },
            "geometry": {
                "X": 16596.166,
                "Y": 36874.918
            }
        },
        {
            "attribute": {
                "OUTLET_NAME": "Marine Parade Polyclinic (Singhealth)"
            },
            "geometry": {
                "X": 36304.0234,
                "Y": 31631.8926
            }
        },
        {
            "attribute": {
                "OUTLET_NAME": "Marine Parade Polyclinic (Singhealth)"
            },
            "geometry": {
                "X": 28576.2051,
                "Y": 29132.0039
            }
        },
        {
            "attribute": {
                "OUTLET_NAME": "Tampines Polyclinic (Singhealth)"
            },
            "geometry": {
                "X": 40523.01,
                "Y": 37716.5547
            }
        },
        {
            "attribute": {
                "OUTLET_NAME": "Toa Payoh Polyclinic (NHG)"
            },
            "geometry": {
                "X": 30854.1777,
                "Y": 35203.2422
            }
        },
        {
            "attribute": {
                "OUTLET_NAME": "Woodlands Polyclinic (NHG)"
            },
            "geometry": {
                "X": 21546.4063,
                "Y": 45844.5977
            }
        },
        {
            "attribute": {
                "OUTLET_NAME": "Yishun Polyclinic (NHG)"
            },
            "geometry": {
                "X": 28427.4941,
                "Y": 45016.31
            }
        },
        {
            "attribute": {
                "OUTLET_NAME": "Pasir Ris Polyclinic (Singhealth)"
            },
            "geometry": {
                "X": 42028.0156,
                "Y": 38950.3164
            }
        },
        {
            "attribute": {
                "OUTLET_NAME": "Queenstown Polyclinic (Singhealth)"
            },
            "geometry": {
                "X": 24412.3555,
                "Y": 31231.8438
            }
        },
        {
            "attribute": {
                "OUTLET_NAME": "Sengkang Polyclinic (Singhealth)"
            },
            "geometry": {
                "X": 34765.73,
                "Y": 41634.793
            }
        }
    ]
},
"field": {
    "fi": [
        {
            "alias": "OUTLET_Name",
            "length": 200,
            "name": "OUTLET_NAME",
            "type": "esriFieldTypeString"
        }
    ]
},
"geometryType": "esriGeometryPoint",
"wkids": {
    "wkid": 3414
}

}

我要实现的是以下JSON格式:

{
"displayFieldName": "OUTLET_NAME",
"fieldAliases": {
    "OUTLET_NAME": "OUTLET_NAME"
},
"geometryType": "esriGeometryPoint",
"spatialReference": {
    "wkid": 3414
},
"fields": [
    {
        "name": "OUTLET_NAME",
        "type": "esriFieldTypeString",
        "alias": "OUTLET_NAME",
        "length": 200
    }
],
"features": [
    {
        "attributes": {
            "OUTLET_NAME": "Alexandra Hospital"
        },
        "geometry": {
            "x": 24412.561499999836,
            "y": 29892.22849999927
        }
    },
    {
        "attributes": {
            "OUTLET_NAME": "Changi General Hospital"
        },
        "geometry": {
            "x": 40924.62980000023,
            "y": 35863.99689999968
        }
    },
    {
        "attributes": {
            "OUTLET_NAME": "National University Hospital"
        },
        "geometry": {
            "x": 22393.636400000192,
            "y": 30731.8563000001
        }
    },
    {
        "attributes": {
            "OUTLET_NAME": "Singapore General Hospital"
        },
        "geometry": {
            "x": 28262.58440000005,
            "y": 29180.98860000074
        }
    },
    {
        "attributes": {
            "OUTLET_NAME": "Tan Tock Seng Hospital"
        },
        "geometry": {
            "x": 29668.50810000021,
            "y": 33693.00019999966
        }
    },
    {
        "attributes": {
            "OUTLET_NAME": "KK Women's and Children's Hospital"
        },
        "geometry": {
            "x": 29509.814699999988,
            "y": 32540.03519999981
        }
    },
    {
        "attributes": {
            "OUTLET_NAME": "Adam Road Hospital"
        },
        "geometry": {
            "x": 25778.033800000325,
            "y": 34343.84779999964
        }
    },
    {
        "attributes": {
            "OUTLET_NAME": "Mount Alvernia Hospital"
        },
        "geometry": {
            "x": 28502.294900000095,
            "y": 35975.92449999973
        }
    },
    {
        "attributes": {
            "OUTLET_NAME": "Paragon Medical Centre"
        },
        "geometry": {
            "x": 28230.75270000007,
            "y": 31911.52370000072
        }
    },
    {
        "attributes": {
            "OUTLET_NAME": "Raffles Hospital"
        },
        "geometry": {
            "x": 30665.577200000174,
            "y": 31499.313200000674
        }
    },
    {
        "attributes": {
            "OUTLET_NAME": "Thomson Medical Centre"
        },
        "geometry": {
            "x": 28906.391900000162,
            "y": 34174.550200000405
        }
    },
    {
        "attributes": {
            "OUTLET_NAME": "Mount Elizabeth Hospital"
        },
        "geometry": {
            "x": 28227.747800000012,
            "y": 31941.378900000826
        }
    }
]
}

这是我在WCF服务中返回JSON的方法。

    [OperationContract]
[WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "json")]
Container GetClinicInfo();

 [DataContract]
public class Container
{
[DataMember]
public string displayFieldName { get; set; }
[DataMember]
public FieldAliases alias { get; set; }
[DataMember]
public string geometryType { get; set; }
[DataMember]
public SpatialReference wkids { get; set; }
[DataMember]
public fieldContainer field { get; set; }
[DataMember]
public Features feature { get; set; }

[DataContract]
public class Container
 {
    [DataMember]
    public string displayFieldName { get; set; }
    [DataMember]
    public FieldAliases alias { get; set; }
    [DataMember]
     public string geometryType { get; set; }
    [DataMember]
     public SpatialReference wkids { get; set; }
    [DataMember]
    public fieldContainer field { get; set; }
    [DataMember]
    public Features feature { get; set; }
public Container(string display,FieldAliases aliases,string type,SpatialReference spatial,fieldContainer container, Features fea)
{
    displayFieldName = display;
    alias = aliases;
    geometryType = type;
    wkids = spatial;
    field = container;
    feature = fea;
}

现在,JSON正在按字母顺序解析"Container";

附图:http://img600.imageshack.us/img600/2861/ehaehaehaeh.png)

有人知道我应该怎么做吗?

重新排序类中的对象

您可以使用[DataMember]属性的Order属性来定义字段序列化的顺序:

[DataContract]
public class Container
{
    [DataMember(Order = 1)]
    public string displayFieldName { get; set; }
    [DataMember(Order = 2)]
    public FieldAliases fieldAliases { get; set; }
    [DataMember(Order = 3)]
    public string geometryType { get; set; }
    [DataMember(Order = 4)]
    public SpatialReference spatialReference { get; set; }
    [DataMember(Order = 5)]
    public fieldContainer fields { get; set; }
    [DataMember(Order = 6)]
    public Features features { get; set; }
}