在API响应使用对象 [英] Using object in API response

查看:148
本文介绍了在API响应使用对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立响应GET请求一个API。一个字段是一个对象,它需要与多次查询返回的数据进行重复。

I'm building a API that responds to GET requests. One field is a object and it needs to be repeated with data for as many times as the query returns.

谁能给我一个如何在C#中的响应使用对象的例子吗?另外,类必须建立?

Can anyone give me a example of how to use a object in a response with c#? Also the class need to be create?

先谢谢了。

地址:

我有这样的结构:

public class root
{
public int model { get; set; }
public string lang { get; set; }
public part[] parts { get; set; }
}

public class part
{
public int id { get; set; }
public string name { get; set; }
public type part_types { get; set; }
}

public class type
{
public string url { get; set; }
public string desc { get; set; }
}

和响应回来为

{模式:4,郎:EN_US,零件:[{ID:1545年,名:第1部分,part_types:{URL:部分.COM / TYPE1,说明:有6位}}]}

{ "model" : 4 , "lang" : "en_US", "parts" : [ { "id" : 1545, "name" : "Part 1", "part_types" : { "url" : "part.com/type1", "desc" : "has 6 bits" } } ] }

但我需要它像

{模式:4,郎:EN_US,零件:[{ID:1545年,名:第1部分,part_types:{1型:{ URL:part.com/type1,说明:有6位},2型:{URL:part.com/type2,说明:有7位。 }}}]}

{ "model" : 4 , "lang" : "en_US", "parts" : [ { "id" : 1545, "name" : "Part 1", "part_types" : { "type 1" : { "url" : "part.com/type1", "desc" : "has 6 bits" }, "type 2" : { "url" : "part.com/type2", "desc" : "has 7 bits." } } } ] }

该零件类型字段是对象,我做了一个叫类型的类。但是我需要有一个或多个类型,并指定类型,例如1型的名称,然后有2个以上的字段下它的URL和说明。正如你可以看到上面有2型,1型和2型。

The part_type field is the object and I made a class called type. But I need to have one or many type and specify the name of the type eg "type 1" then have 2 more fields under it url and desc. As you can see above has 2 type, type 1 and type 2.

谁能帮我在哪里,我错了?

Can anyone help me where I am going wrong?

推荐答案

所以,如果我理解正确的话,你要part_types,以便能够有很多地方?在当前的code,A part_types对象只能有一个。

So, if I understand correctly, you want "part_types" to be able to have many parts? In your current code, a part_types object can only have one.

您需要两样东西:
1.数据类型是一个集合,
2.将这些数据写入输入你想要的方式序列化器。
因此,例如,你可以使用一个词典(System.Collections.Generic.Dictionary)。

You need two things: 1. A data type that is a collection, and 2. A serializer that writes that data type the way you want. So, for example, you can use a Dictionary (System.Collections.Generic.Dictionary).

public Dictionary<string,type> part_types { get; set; }

因此​​,假设你已经创建的对象Type1和Type2,你可以这样写:

So, assuming you have created objects type1 and type2, you would write:

mypart.part_types = new Dictionary<string,type>();
mypart.part_types.Add("type 1", type1);
mypart.part_types.Add("type 2", type2);

现在会发生什么取决于串行器。我通常使用Json.NET,它运作良好,并是免费的。我建立了我的对象类似,但使用字典,我也得到:

Now what happens depends on the serializer. I typically use Json.NET, which works well and is free. I set up my objects like yours but using the dictionary, and I get:

{
  "model": 4,
  "lang": "en_US",
  "parts": [
    {
      "id": 1545,
      "name": "Part 1",
      "part_types": {
        "type 1": {
          "url": "part.com/type1",
          "desc": "has 6 bits"
        },
        "type 2": {
          "url": "part.com/type1",
          "desc": "has 6 bits"
        }
      }
    }
  ]
}

我想这就是你要找的......但是,使用DataContractJsonSerializer(System.Runtime.Serialization.Json.DataContractJsonSerializer),我得到这样的:

I think that's what you're looking for... HOWEVER, using the DataContractJsonSerializer (System.Runtime.Serialization.Json.DataContractJsonSerializer), I get this:

{
  "lang": "en_US",
  "model": 4,
  "parts": [
    {
      "id": 1545,
      "name": "Part 1",
      "part_types": [
        { "Key" : "type 1", "Value": {"desc":"has 6 bits","url":"part.com\/type1"} },
        { "Key" : "type 2", "Value": {"desc":"has 6 bits","url":"part.com\/type1"} }
      ]
    }
  ]
}

我不知道的DataContractJsonSerializer如何处理其他集合(如列表,KeyValuePairs等),所以如果你使用它,你可能需要试验。

I'm not sure how the DataContractJsonSerializer handles other collections (e.g. Lists, KeyValuePairs, etc.) so if you're using it, you may need to experiment.

这篇关于在API响应使用对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆