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

查看:39
本文介绍了在 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?

提前致谢.

添加:

到目前为止,我的结构如下:

so far I have a structure like:

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; }
}

返回的响应为

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

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

但我需要它

{ "model" : 4 , "lang" : "en_US", "parts" : [ { "id" : 1545, "name" : "Part 1", "part_types" : { "type 1" : {"url" : "part.com/type1", "desc" : "有 6 位" }, "type 2" : { "url" : "part.com/type2", "desc" : "有 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." } } } ] }

part_type 字段是对象,我创建了一个名为 type 的类.但是我需要有一种或多种类型并指定类型的名称,例如类型 1",然后在它的 url 和 desc 下还有 2 个字段.如您所见,上面有 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"能够有多个部分?在您当前的代码中,一个 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天全站免登陆