我可以在C#中使用JSON字符串吗?

我可以在C#中使用JSON字符串吗?,c#,arrays,json,visual-studio-2017,C#,Arrays,Json,Visual Studio 2017,正在为我在Windows Visual Studio中构建的android重建java应用程序。需要有关在Visual Studio Visual C#Forms应用程序(.NET Framework)中使用JSON字符串的帮助 我正在创建一种新的文件格式,以便能够将数据传输到我公司的不同机器人。我为我的android应用程序使用了一个列表映射,该文件包含一个JSON字符串。是否可以将字符串添加到Visual C#Forms(.NET Framework)的列表中,以便在列表框中查看?提供样品

正在为我在Windows Visual Studio中构建的android重建java应用程序。需要有关在Visual Studio Visual C#Forms应用程序(.NET Framework)中使用JSON字符串的帮助

我正在创建一种新的文件格式,以便能够将数据传输到我公司的不同机器人。我为我的android应用程序使用了一个列表映射,该文件包含一个JSON字符串。是否可以将字符串添加到Visual C#Forms(.NET Framework)的列表中,以便在列表框中查看?提供样品

[{"VALUE":"03","ATTRIBUTE":"Laayelbxw"},
 {"VALUE":"01","ATTRIBUTE":"Leruaret"},
 {"VALUE":"08","ATTRIBUTE":"Lscwbryeiyabwaa"},
 {"VALUE":"09","ATTRIBUTE":"Leruxyklrwbwaa"}]
你当然可以

我所知道的在C#中反序列化JSON的最简单方法是使用

例如:

/*
 * This class represent a single item of your collection.
 * It has the same properties name than your JSON string members
 * You can use differents properties names, but you'll have to use attributes
 */
class MyClass
{
    public int VALUE { get; set; }
    public string ATTRIBUTE { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        var myJSON = "[{\"VALUE\":\"03\",\"ATTRIBUTE\":\"Laayelbxw\"},{\"VALUE\":\"01\",\"ATTRIBUTE\":\"Leruaret\"},{\"VALUE\":\"08\",\"ATTRIBUTE\":\"Lscwbryeiyabwaa\"},{\"VALUE\":\"09\",\"ATTRIBUTE\":\"Leruxyklrwbwaa\"}]";

        //                 V---------V----- Namespace is Newtonsoft.Json
        var MyCollection = JsonConvert.DeserializeObject<List<MyClass>>(myJSON);
        // Tadaam ! You now have a collection of MyClass objects created from that json string

        foreach (var item in MyCollection)
        {
            Console.WriteLine("Value : " + item.VALUE);
            Console.WriteLine("Attribute : " + item.ATTRIBUTE);
        }
        Console.Read();
    }
}

会是这样的

public class JsonExample
{
    public int VALUE { get; set; }

    public string ATTRIBUTE { get; set; }
}

public void GetJson()
{
    string json = "your string";
    var xpto = JsonConvert.DeserializeObject<List<JsonExample>>(json);
}
public类JsonExample
{
公共int值{get;set;}
公共字符串属性{get;set;}
}
public void GetJson()
{
string json=“您的字符串”;
var xpto=JsonConvert.DeserializeObject(json);
}

可以。添加重要注意事项可能会重复:请确保对象的所有属性都已实例化,因为如果其中任何属性未设置为引用,JsonConvert.DeserializeObject()将失败。@MahdiBenaoun“对象的所有属性都已实例化”是什么意思?你是说所有的json成员都设置好了吗?谢谢你给了我一个比我更好的答案,解释得很好^^我很想知道为什么我被否决了。有什么线索吗?我也很感兴趣,希望@downvoter解释一下,如果有什么问题,可能是因为重复,你这么认为吗?你的答案也很好,当你试图发帖的时候,@Cid自己发帖的速度比你快。是的,我已经快了一点,但你仍然应该得到一次提升投票,因为你的答案是准确的
public class JsonExample
{
    public int VALUE { get; set; }

    public string ATTRIBUTE { get; set; }
}

public void GetJson()
{
    string json = "your string";
    var xpto = JsonConvert.DeserializeObject<List<JsonExample>>(json);
}