标签: deserialization

在 C# 中反序列化 JSON - 有什么问题?

我试图在 c# 中反序列化这个 JSON,但没有成功:

{
    "settings": {
        "path": "http:\/\/www.igormasin.it\/fileuploads\/tanja_23a6id"
    },
    "files": [{
        "file": "\/IMG_0992-Edit_a.jpg"
    }, {
        "file": "\/IMG_1024-Edit_a.jpg"
    }, {
        "file": "\/IMG_1074-Edit_a.jpg"
    }, {
        "file": "\/Untitled-1.jpg"
    }]
}
Run Code Online (Sandbox Code Playgroud)

我的代码:

public class JsonTxt
{
    public IList<string> settings { get; set; }
    public IList<string> files { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

downloadstring 包含 Json 文本:

 var deserialized = JsonConvert.DeserializeObject<JsonTxt>(downloadString);        
 Console.WriteLine("*************************************************");
 Console.WriteLine(deserialized.settings[0].ToString());
 Console.WriteLine(deserialized.files.Count);
Run Code Online (Sandbox Code Playgroud)

例外:

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. 
{"name":"value"}) into type 'System.Collections.Generic.IList`1[System.String]' because the type 
requires a JSON array (e.g. …
Run Code Online (Sandbox Code Playgroud)

c# json deserialization

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

C# JSON 反序列化器返回空对象列表

我正在尝试将 json 对象数组转换为 C# 列表,但无法使其工作。目前,我已经做了这门课:

public class FineModel
{
    public String officer { get; internal set; }
    public String target { get; internal set; }
    public int amount { get; internal set; }
    public String reason { get; internal set; }
    public String date { get; internal set; }

    public FineModel() { }
}
Run Code Online (Sandbox Code Playgroud)

现在,我想要反序列化这个 JSON,它的格式似乎正确:

[  
   {  
      "officer":"Alessia Smith",
      "target":"Scott Turner",
      "amount":1800,
      "reason":"test",
      "date":"9/4/2017 3:32:04 AM"
   }
]
Run Code Online (Sandbox Code Playgroud)

应该发挥作用的 C# 行是:

List<FineModel> removedFines = JsonConvert.DeserializeObject<List<FineModel>>(json);
Run Code Online (Sandbox Code Playgroud)

它返回一个对象,但是当我尝试打印它的值时,它返回 amount 属性为 0 …

c# json deserialization

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

使用字符串.NET命名变量

我正在研究.NET中的反序列化类,我必须开发一个方法,它为我提供了一个存储在字符串中的变量名.

我有一个字符串,如:

string string_name = "this_is_going_to_be_var_name";
Run Code Online (Sandbox Code Playgroud)

现在我该怎么做才能让我的代码动态声明一个名为this_is_going_to_be_var_name的变量?

因此要清理:将有一个反序列化类,它将根据高级程序员/用户的意愿,声明与作为输入提供的字符串相同的变量及其PARENT TYPES.

例如:在javascript/jQuery中,当我通过发出请求来获取JSON时,解释器声明具有相同名称的变量/数组并为它们赋值.如果{"var_name":"var_value"}是一个JSON字符串,则解释器将创建一个名为var_name的变量,并为其分配"var_value",例如json_data_object.var_name.

.net c# .net-4.0 deserialization

-4
推荐指数
1
解决办法
5708
查看次数

反序列化JSON数字返回错误的值

我想json strings用go语言反序列化。不同键的值类型不同。例如string {\"category\":\"6\",\"cid\":2511993760745787586}category类型是stringcid类型是int64

我的代码如下:

func main() {
    oriInfo := make([]interface{}, 0)
    pickled := "[{\"category\":\"6\",\"cid\":2511993760745787586},{\"category\":\"5\",\"cid\":2504429915944783937}]"
    err := json.Unmarshal([]byte(pickled), &oriInfo)
    if err != nil {
        fmt.Println(err)
        return
    }
    all := make([]map[string]interface{}, 0, len(oriInfo))
    for _, val := range oriInfo {
        m := make(map[string]interface{})
        for k, v := range val.(map[string]interface{}) {
            switch k {
            case "category":
                m[k] = v.(string)
            case "cid":
                m[k] = int64(v.(float64))
            }
        }
        all = append(all, m)
    } …
Run Code Online (Sandbox Code Playgroud)

json go deserialization

-4
推荐指数
1
解决办法
447
查看次数

标签 统计

deserialization ×4

c# ×3

json ×3

.net ×1

.net-4.0 ×1

go ×1