| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3330 人关注过本帖
标题:请问一下,C#中RestSharp组件发Jason串如何添加列表元素。
只看楼主 加入收藏
S140131022
Rank: 2
来 自:重庆邮电大学
等 级:论坛游民
帖 子:205
专家分:35
注 册:2014-10-9
结帖率:90.24%
收藏
已结贴  问题点数:20 回复次数:5 
请问一下,C#中RestSharp组件发Jason串如何添加列表元素。
程序代码:
{
    "credential": {
        "user_id": "107815",
        "session_id": "979457350307389",
        "account_id": "C-107815-0"
    },
    "request_object": {
        "vendor_id_list": [
            "5Q01200000000051",
            "5Q01200000000052"
        ]
    }
}


就类似上面的 vendor_id_list, 发送端怎么写入到jason串中呢, 对于非jason串, 其写入方式如下:

var body = new { credential = new { user_id = UIGlobal.Config.UserNo, session_id = m_SessionId, account_id = acountNo }, request_object = new { vendor_id_list = Customer } };
                GwDll.InputUpperLog(body.ToString(), UIGlobal.UPPER_SEND);       // 上手发送日志
                request.AddParameter("application/json; charset=utf-8", JsonConvert.SerializeObject(body), ParameterType.RequestBody);
搜索更多相关主题的帖子: body 组件 new 列表 元素 
2021-06-07 12:24
S140131022
Rank: 2
来 自:重庆邮电大学
等 级:论坛游民
帖 子:205
专家分:35
注 册:2014-10-9
收藏
得分:0 
有人在吗?

既然还有不甘心
就还没到放弃的时候~
2021-06-07 13:32
qq2889577966
Rank: 4
等 级:业余侠客
威 望:5
帖 子:68
专家分:287
注 册:2021-4-14
收藏
得分:20 
把list序列化为json,json反序列化为list
程序代码:
// "引用"添加System.Runtime.Serialization
using System.Runtime.Serialization.Json;
using using System.Xml.Serialization;
using System.Xml;
using System.Runtime.Serialization.Formatters.Binary;

public class Serializer
{
    /// 将对象序列化为json文件
    public static void ObjectToJson<T>(T t, string path) where T : class
    {
        DataContractJsonSerializer formatter = new DataContractJsonSerializer(typeof(T));
        using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate))
        {
            formatter.WriteObject(stream, t);
        }
    }

    /// 将对象序列化为json字符串
    public static string ObjectToJson<T>(T t) where T : class
    {
        DataContractJsonSerializer formatter = new DataContractJsonSerializer(typeof(T));
        using (MemoryStream stream = new MemoryStream())
        {
            formatter.WriteObject(stream, t);
            string result = System.Text.Encoding.UTF8.GetString(stream.ToArray());
            return result;
        }
    }

    /// json字符串转成对象
    public static T JsonToObject<T>(string json) where T : class
    {
        DataContractJsonSerializer formatter = new DataContractJsonSerializer(typeof(T));
        using (MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(json)))
        {
            T result = formatter.ReadObject(stream) as T;
            return result;
        }
    }

    /// 将对象序列化为xml文件
    public static void ObjectToXml<T>(T t, string path) where T : class
    {
        XmlSerializer formatter = new XmlSerializer(typeof(T));
        //using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate))
        using (FileStream stream = new FileStream(path, FileMode.Create))
        {
            formatter.Serialize(stream, t);
        }
    }

    /// 将对象序列化为xml字符串
    public static string ObjectToXml<T>(T t) where T : class
    {
        XmlSerializer formatter = new XmlSerializer(typeof(T));
        using (MemoryStream stream = new MemoryStream())
        {
            formatter.Serialize(stream, t);
            string result = System.Text.Encoding.UTF8.GetString(stream.ToArray());
            return result;
        }
    }

    /// 将xml文件反序列化为对象
    public static T XmlToObject<T>(T t, string path) where T : class
    {
        XmlSerializer formatter = new XmlSerializer(typeof(T));
        using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate))
        //using (FileStream stream = new FileStream(path, FileMode.Create))
        {
            XmlReader xmlReader = new XmlTextReader(stream);
            T result = formatter.Deserialize(xmlReader) as T;
            return result;
        }
    }

    /// 将xml字符串反序列化为对象
    public static T XmlStrToObject<T>(T t, string str) where T : class
    {
        XmlSerializer formatter = new XmlSerializer(typeof(T));

        StringReader xmlReader = new StringReader(str);
        T result = formatter.Deserialize(xmlReader) as T;
        return result;
    }


    /// 将对象序列化为二进制流
    public static byte[] ObjectToString<T>(T t)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        using (MemoryStream stream = new MemoryStream())
        {
            formatter.Serialize(stream, t);
            byte[] result = stream.ToArray();
            return result;
        }
    }

    /// 将二进制流反序列为类型
    public static T StringToObject<T>(byte[] bytes) where T : class
    {
        BinaryFormatter formatter = new BinaryFormatter();
        using (MemoryStream stream = new MemoryStream(bytes, 0, bytes.Length))
        {
            T result = formatter.Deserialize(stream) as T;
            return result;
        }
    }

    /// 将对象序列化为文件
    public static void ObjectToFile<T>(T t, string path)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate))
        {
            formatter.Serialize(stream, t);
            stream.Flush();
        }
    }

    /// 将文件反序列化为对象
    public static T FileToObject<T>(string path) where T : class
    {
        using (FileStream stream = new FileStream(path, FileMode.Open))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            T result = formatter.Deserialize(stream) as T;
            return result;
        }
    }
}
2021-06-07 19:50
qq2889577966
Rank: 4
等 级:业余侠客
威 望:5
帖 子:68
专家分:287
注 册:2021-4-14
收藏
得分:0 
比如
类,(要有[DataContract])
程序代码:

 [DataContract]
    public class File_Serializable
    {
        [DataMember]
        public List<Serializable_BandObject> serializable_BandObjects = new List<Serializable_BandObject>();
        [DataMember]
        public List<Serializable_ControlObject> serializable_ControlObjects = new List<Serializable_ControlObject>();
        [DataMember]
        public DateTime created_Time;
        [DataMember]
        public string page_Name;
        [DataMember]
        public int Current_Num;
        [DataMember]
        public int Pcode_Type = 0;
        [DataMember]
        public Paper_TypeFace paper_TypeFace = new Paper_TypeFace();
        [DataMember]
        public DataBaseType dataBaseType = new DataBaseType();
        [DataMember]
        public string base_TableName;
        [DataMember]
        public string sql_String;
    }
    public static List<File_Serializable> file_Serializables = new List<File_Serializable>();


序列化为xml
file_Serializables = Serializer.XmlToObject(file_Serializables, 文件);
结合楼上的,json一样用法
2021-06-07 19:56
S140131022
Rank: 2
来 自:重庆邮电大学
等 级:论坛游民
帖 子:205
专家分:35
注 册:2014-10-9
收藏
得分:0 
回复 楼主 S140131022
程序代码:
// main.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>

using namespace std;

class people
{
public:
    int test()
    {
        static bool flag = false;
        if (!flag)
        {
            std::cout << "Haha" << std::endl;
            flag = true;
        }
        return 0;
    }
};

int test()
{
    static bool flag = false;
    if (!flag)
    {
        std::cout << "Haha" << std::endl;
        flag = true;
    }
    return 0;
}

int main()
{
    for (int i = 0; i < 10; i++)
    {
        test();
    }

    people* pp = new people();
    pp->test();

    getchar();
    return 0;
}


既然还有不甘心
就还没到放弃的时候~
2021-06-11 10:51
S140131022
Rank: 2
来 自:重庆邮电大学
等 级:论坛游民
帖 子:205
专家分:35
注 册:2014-10-9
收藏
得分:0 
回复 4楼 qq2889577966
非常感谢大佬!

既然还有不甘心
就还没到放弃的时候~
2021-06-11 10:53
快速回复:请问一下,C#中RestSharp组件发Jason串如何添加列表元素。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.056828 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved