请帮我从这个JSON字符串中找出column1的值:
{"company":{"oltpid":{"Column1":"345353"}}}
...使用C#.net。我尝试使用以下代码。但没有得到column1的值:
RootObject result = new JavaScriptSerializer().Deserialize<RootObject>(JsonString);
public class RootObject
{
public company[] companys;
public class company
{
public oltpid[] oltp_id;
public class oltpid
{
public String Column1;
};
};
}
答案 0 :(得分:4)
您的json对象中没有数组,因此您也不应在对象中使用它们。您还需要使用属性而不是变量,因为JSON序列化程序可以使用属性。尝试以下结构:
public class RootObject
{
public company company { get; set; }
}
public class company
{
public oltpid oltpid { get; set; }
}
public class oltpid
{
public String Column1 { get; set; }
}
然后,您可以使用以下代码获取Column1
的值:
RootObject result = new JavaScriptSerializer().Deserialize<RootObject>(JsonString);
var yourValue = result.company.oltpid.Column1;
答案 1 :(得分:1)
我用json.net
给你一个有效的解决方案从NuGet
安装json.netInstall-Package Newtonsoft.Json
这里是代码
public class RootObject
{
public Company company { get; set; }
}
public class Company
{
public Oltpid oltpid { get; set; }
}
public class Oltpid
{
public string Column1 { get; set; }
}
var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(json);
var result = obj.company.oltpid.Column1