Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从json格式对象获取值_C#_Javascript - Fatal编程技术网

C# 从json格式对象获取值

C# 从json格式对象获取值,c#,javascript,C#,Javascript,在我的asp.Net应用程序(MVC3)视图中,我正在使用一些jquery控件。我得到了json { "text":"Books", "state":"open", "attributes":{ "url":"/demo/book/abc", "price":100 } 如何获取属性名的值?您可以使用objectName[attributename]

在我的asp.Net应用程序(MVC3)视图中,我正在使用一些jquery控件。我得到了json

 {  
        "text":"Books",  
        "state":"open",  
        "attributes":{  
            "url":"/demo/book/abc",  
            "price":100  
        }    

如何获取属性名的值?

您可以使用
objectName[attributename]
objectName.attributename

比如说

var test =  {  
        "text":"Books",  
        "state":"open",  
        "attributes":{  
            "url":"/demo/book/abc",  
            "price":100  
        }  

test.text
将返回值

我认为您需要

var json =  {   
    "text":"Books",   
    "state":"open",   
    "attributes":{   
        "url":"/demo/book/abc",   
        "price":100   
    } 
json.attributes.url

例如。

如果你

var data = {  
        "text":"Books",  
        "state":"open",  
        "attributes":{  
            "url":"/demo/book/abc",  
            "price":100  
        }    
如果属性的属性没有改变,您可以使用@furqan所说的
data.attributes.url

但是如果你的属性可以改变,你可以像这样迭代属性

    for(x in data.attrubutes)
    {
    //do some specific code for x which will be the NAME of the attribute.
//by calling data.attributes[x] for the value.
    }

请尝试
data.attributes.url
,其中数据是您的json对象。谢谢,但它不起作用,elemet有属性,有带值的键url,但这不起作用。我刚刚尝试过,从我的调试器json.attributes.url“/demo/book/abc”字符串中,它看起来不错。如果您想发布更多的代码来提供一些上下文,请这样做。