C# - 使用冒号解析动态和访问属性名称的JSON

时间:2016-02-17 15:29:24

标签: c# json dynamic json.net

获取带有属性的JSON时: 即。

class MyNode: ASCellNode {
     var myText: ASTextNode

    init() {
        myText = ASTextNode()
        addSubnode(myText)
    }

    override func layoutSpecThatFits(constrainedSize: ASSizeRange) -> ASLayoutSpec {

        let cellWidth = UIScreen.mainScreen().bounds.size.width
        myText.sizeRange = ASRelativeSizeRangeMake(
            ASRelativeSize(
                width: ASRelativeDimensionMakeWithPercent(0),
                height: ASRelativeDimensionMakeWithPoints(0)),
            ASRelativeSize(
                width: ASRelativeDimensionMakeWithPercent(cellWidth),
                height: ASRelativeDimensionMakeWithPoints(200)))

        let insetSpecs = ASInsetLayoutSpec(
            insets: UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8),
            child: myText)

        return insetSpecs
     }
 }

我正在使用Newtonsoft动态解析。

var randomColors    = ["green", "red", "white"];
var clrI    = 0;
$('#carousel-example-generic').on('slide.bs.carousel', function(e) {
    $(this).css('background-color', randomColors[clrI]);
    clrI++;
    if(clrI>=randomColors.length) {
        clrI    = 0;
    }
});

我正在尝试访问

{
    "root": {
        "sim:names": [{
            "name": "Tom"
            },
            {
            "name": "David"
            }]  
    }
}

但是编译错误“无效的表达式术语':'”

我如何访问它?

1 个答案:

答案 0 :(得分:0)

您应该将其转换为对象

var data = JsonConvert.DeserializeObject<Object>(jsonString);

然后像这样访问:

var value = ((JObject)data)["root"]["sim:names"];