相关疑难解决方法(0)

在C#中将对象转换为JSON字符串

可能重复:
将C#对象转换为.NET 4中的JSON字符串

在Java中,我有一个代码将java对象转换为JSON字符串.如何在C#中做类似的事情?我应该使用哪个JSON库?

谢谢.

JAVA代码

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class ReturnData {
    int total;

    List<ExceptionReport> exceptionReportList;  

    public String getJSon(){
        JSONObject json = new JSONObject(); 

        json.put("totalCount", total);

        JSONArray jsonArray = new JSONArray();
        for(ExceptionReport report : exceptionReportList){
            JSONObject jsonTmp = new JSONObject();
            jsonTmp.put("reportId", report.getReportId());      
            jsonTmp.put("message", report.getMessage());            
            jsonArray.add(jsonTmp);         
        }

        json.put("reports", jsonArray);
        return json.toString();
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

c# json

49
推荐指数
2
解决办法
25万
查看次数

用C#调用json

我正在尝试使用C#进行json调用.我开始创建一个电话,但它不起作用:

public bool SendAnSMSMessage(string message)
{
    HttpWebRequest request = (HttpWebRequest)
                             WebRequest.Create("http://api.pennysms.com/jsonrpc");
    request.Method = "POST";
    request.ContentType = "application/json";

    string json = "{ \"method\": \"send\", "+
                  "  \"params\": [ "+
                  "             \"IPutAGuidHere\", "+
                  "             \"msg@MyCompany.com\", "+
                  "             \"MyTenDigitNumberWasHere\", "+
                  "             \""+message+"\" " +
                  "             ] "+
                  "}";

    StreamWriter writer = new StreamWriter(request.GetRequestStream());
    writer.Write(json);
    writer.Close();

    return true;
}
Run Code Online (Sandbox Code Playgroud)

任何关于如何使这项工作的建议将不胜感激.

c# json httpwebrequest

34
推荐指数
3
解决办法
12万
查看次数

如何返回JSon对象

我正在使用一个jQuery插件,需要一个具有以下结构的JSON对象(我将从数据库中检索值):

{ results: [
    { id: "1", value: "ABC", info: "ABC" },
    { id: "2", value: "JKL", info: "JKL" },
    { id: "3", value: "XYZ", info: "XYZ" }
] }
Run Code Online (Sandbox Code Playgroud)

这是我的班级:

public class results
{
    int _id;
    string _value;
    string _info;

    public int id
    {
        get
        {
            return _id;
        }
        set
        {
            _id = value;
        }
    }
    public string value
    {
        get
        {
            return _value;
        }
        set
        {
            _value = value;
        }
    }
    public string info
    {
        get
        {
            return …
Run Code Online (Sandbox Code Playgroud)

c# json json.net

24
推荐指数
2
解决办法
14万
查看次数

泛型/ JSON JavaScriptSerializer C#

我正在使用VS2008Express在NET3.5SP1中构建一个winForms应用程序.我试图使用System.Web.Script.Serialization库反序列化对象.

错误是:类型'jsonWinForm.Category'不支持反序列化数组.

干杯!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;

namespace jsonWinForm {
    public class Category
    {
        public int categoryid;
        public string name;
        public int serverimageid;
        public DateTime dateuploaded;
        public bool enabled;
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (WebClient client = new WebClient())
            {
                //manipulate request headers (optional) …
Run Code Online (Sandbox Code Playgroud)

c# json

11
推荐指数
2
解决办法
3万
查看次数

无法加载文件或程序集'System.Web.Extensions,Version = 1.0.61025.0错误

我已经检查了这个网站的解决方案,但我找不到可以帮助我的东西..我的主机在他们的服务器上Diabled自定义错误,所以现在我可以看到真正的错误.

我的网站在旧主机上运行完美,但现在我搬到了新主机

请注意:旧的主机是一个VPSNetVison主机

所以我停下来支付他,现在我作为共享主机同一个NetVision主机上托管.

所以我将所有文件移动到根文件夹并在Web.config中配置SQL详细信息

我出于某种原因无法加载网站.我一直都有错误. 请帮我!

我得到的错误:

'/'应用程序中的服务器错误.

配置错误

分析器错误消息:

Could not load file or assembly 'System.Web.Extensions,
Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of
its dependencies. The system cannot find the file specified.
Run Code Online (Sandbox Code Playgroud)

来源错误:

Line 49:            <assemblies>
Line 50:                <add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
Line 51:                <add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>*
Line 52:            </assemblies>
Line 53:        </compilation>
Run Code Online (Sandbox Code Playgroud)

源文件: …

asp.net system.web.extensions

11
推荐指数
2
解决办法
8万
查看次数

将C#对象转换为Json对象

我试图将C#对象序列化为Json对象.然后将其提交给Salesforce API,并创建一个应用程序.现在我将C#对象序列化为Json字符串,但我需要它作为一个对象.

这是我的C#对象以及伴随序列化.

Customer application = new Customer { 
    ProductDescription = "gors_descr " + tbDescription.Text, 
    Fname = "b_name_first " + tbFName.Text, 
    Lname = "b_name_last " + tbLName.Text
};

var json = new System.Web.Script.Serialization.JavaScriptSerializer();
string jsonString = json.Serialize(application);

string endPoint = token.instance_url + "/services/apexrest/submitApplication/";    
string response = conn.HttpPost(endPoint, json, token);
Literal rLiteral = this.FindControl("resultLiteral") as Literal;
Run Code Online (Sandbox Code Playgroud)

我需要在JSON对象内输出JSON字符串.我需要的一个例子如下:

"{ \"jsonCreditApplication\" : " +
    "\"gors_descr\" : \"Appliances\", " +
    "\"b_name_first\" : \"Marisol\", " +
    "\"b_name_last\" : \"Testcase\", " +
"}"; 
Run Code Online (Sandbox Code Playgroud)

此硬编码的json字符串位于对象内部.按照目前的情况,C#对象中的值将输出到JSON字符串中,但我需要将其输出到对象中,以便Salesforce API接受提交.

如何将JSON字符串追加或插入对象?

c# api serialization json

8
推荐指数
1
解决办法
6万
查看次数

C#为JSON序列化和反序列化创建.NET对象

我正在为C#中的twitch chat bot开发一个点系统.我需要将点数和小时数的用户列表保存到本地.txt文件中,如下所示:

  • 用户:???
    • 用户名 : ???
      • 尼克:字符串
      • 要点:int
      • 小时:int
    • 用户名 : ???
      • 尼克:字符串
      • 要点:int
      • 小时:int
    • 用户名 : ???
      • 尼克:字符串
      • 要点:int
      • 小时:int

我正在尝试创建一个.NET对象来序列化为一个JSON字符串并将其写入我的.txt文件,但我被困在这里.

我认为用户需要是某种类型的数组,但我如何处理用户名?我不认为JSON支持自定义类型?

谢谢你的时间,X3ntr

c# json

7
推荐指数
2
解决办法
2万
查看次数

如何使用字符串插值和逐字字符串一起创建JSON字符串文字

我正在尝试创建一个表示JSON对象数组的字符串文字,所以我想到使用字符串插值功能,如下面的代码所示:

    public static void MyMethod(string abc, int pqr)
    {
        string p = $"[{{\"Key\":\"{abc}\",\"Value\": {pqr}  }}]";
    }
Run Code Online (Sandbox Code Playgroud)

现在我想到使用逐字字符串,这样我就不必使用反斜杠来转义双引号.所以我通过这个答案来了解逐字字符串和字符串插值可以一起使用.所以我改变了我的代码如下:

public static void MyMethod(string abc, int pqr)
{
    string p = $@"[{{"Key":"{abc}","Value": {pqr} }}]";
}
Run Code Online (Sandbox Code Playgroud)

但它无法编译.谁能帮助我,如果有什么错误的,我使用,否则将能够使用C#字符串逐字功能逃脱在这种情况下双引号?

.net c# string string-interpolation verbatim-string

6
推荐指数
1
解决办法
4324
查看次数

C# HttpClient 在字典字符串/对象中使用 FormUrlEncodedContent 对象发布内容

我正在尝试将内容发布到我的服务器。这就是我过去一直这样做的方式,直到我不得不使用字符串以外的对象为止。

using (HttpClient client = new HttpClient())
{
    client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(authType, tokens);
    var postParams = new Dictionary<string, object>();

    postParams.Add("string", string);
    postParams.Add("int", string);
    postParams.Add("datetime", DateTime);
    postParams.Add("datetime", DateTime);
    postParams.Add("Match", Match);
    postParams.Add("TicketId", token);

    using (var postContent = new FormUrlEncodedContent(postParams.ToDictionary()))
    {
        var myContent = JsonConvert.SerializeObject(postParams);
        var buffer = System.Text.Encoding.UTF8.GetBytes(myContent);
        var byteContent = new ByteArrayContent(buffer);
        byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

        using (HttpResponseMessage response = await client.PostAsync(@"http://url/api", byteContent))
        {
            response.EnsureSuccessStatusCode(); // Throw if httpcode is an error
            using (HttpContent content = response.Content)
            {
                string result = …
Run Code Online (Sandbox Code Playgroud)

c# encoding json http httpclient

3
推荐指数
1
解决办法
8341
查看次数

如何将C#对象传递给$ .ajax

我有简单的JavaScript代码,我想从控制器中获取一个对象并在脚本中解析它,但在脚本中,我无法访问该对象的任何属性.

我的对象(在控制器中):

public class userData
{
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string state { get; set; }
    public bool success { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

脚本:

function  load()
{
    var sender = {
         userNo : $("#userNo").val(),
    }
    $.ajax({
        type : "POST",
        url : "testGetUser",
        data : sender,
        success:function(data)
            {
                if(1)
                {
                    $("#section #result0").html(data.firstName);
                    $("#section #result1").html(data.lastName);
                    $("#section #result2").html(data.state);
                    $("#section").slideDown().delay(1000).slideUp();
                }
            }
    });
}
Run Code Online (Sandbox Code Playgroud)

控制器:

[HttpPost]
public userData testGetUser(long userNo)
{
    userData …
Run Code Online (Sandbox Code Playgroud)

javascript c# ajax asp.net-mvc jquery

2
推荐指数
1
解决办法
1597
查看次数