创建包含数组的JObject时出错

时间:2016-04-29 15:01:47

标签: c# .net json json.net

我正在尝试重新创建这个json:

    Column A            Column B            Column C
    lookup0x16e663000   lookup0x16e663000   1461178793193312
    lookup0x16e663001   lookup0x16e663000   1461178793193508
    lookup0x16e663000   lookup0x16e663001   1461178793193704
    lookup0x16e663001   lookup0x16e663001   1461178793193891
    lookup0x16e663000   lookup0x16e663005   1461178793194110
    lookup0x16e663005   lookup0x16e663005   1461178793194312
    lookup0x16e663005   lookup0x16e663000   1461178793194538
    lookup0x16e663000   lookup0x16e663000   1461178793194760

现在我正在这样做:

{
    "auth": {
        "identity": {
            "methods": [
                "password"
            ],
            "password": {
                "user": {
                    "id": "90551bd1bcf6474dba09c96617a33638",
                    "password": "hoeIX1.i-.M]wiu9"
                }
            }
        },
        "scope": {
            "project": {
                "id": "2440e4fa1725452cb2e14506cb5d63ec"
            }
        }
    }
}

我收到此错误:

  

“无法将Newtonsoft.Json.Linq.JProperty添加到Newtonsoft.Json.Linq.JArray。”

无效的行是:

JObject auth = new JObject(
    new JProperty("auth", new JObject(
        new JProperty("identity", new JObject(
            new JProperty("method",JArray.Parse("[\"password\"]")),
            new JProperty("password", new JObject(
                new JProperty("user", new JObject(
                    new JProperty("id", JValue.CreateString(identityWithProject.Username)),
                    new JProperty("password", JValue.CreateString(identityWithProject.ProjectName))))))),
        new JProperty("scope", new JObject(
            new JProperty("project", new JObject(
                new JProperty("id", JValue.CreateString(identityWithProject.ProjectName))))))))));

如果我删除JArray部分它工作正常,我怎么能修复它以便它与数组部分一起工作? 我发现的大多数例子都是JObjects数组,但实际情况并非如此。 谢谢!

1 个答案:

答案 0 :(得分:3)

看起来错误的),这有效:

var auth = new JObject(
    new JProperty("auth", new JObject(
        new JProperty("identity", new JObject(
            new JProperty("methods", JArray.Parse("[\"password\"]")),
            new JProperty("password", new JObject(
                new JProperty("user", new JObject(
                    new JProperty("id", JValue.CreateString(identityWithProject.Username)),
                    new JProperty("password", JValue.CreateString(identityWithProject.ProjectName)))))))), // 8 x ')'
        new JProperty("scope", new JObject(
            new JProperty("project", new JObject(
                new JProperty("id", JValue.CreateString(identityWithProject.ProjectName))))))))); // 9 x ')'

我刚从最后一行移动了一个)到最后一行的第三行。

此外,如果要将"method"属性设为"methods",则应将其命名为JValue.CreateString。此外,在创建JProperties时没有理由使用JArray.Parse("[\"password\"]"),而@Steven Brickner指出,new JArray("password")可以简化为var auth = new JObject( new JProperty("auth", new JObject( new JProperty("identity", new JObject( new JProperty("methods", new JArray("password")), new JProperty("password", new JObject( new JProperty("user", new JObject( new JProperty("id", identityWithProject.Username), new JProperty("password", identityWithProject.ProjectName) ) ) ) ) ) ), new JProperty("scope", new JObject( new JProperty("project", new JObject( new JProperty("id", identityWithProject.ProjectName) ) ) ) ) ) ) );

它可能更冗长,但为了便于阅读,这里它是完全缩进的:

$subArrOfLive = [];
foreach($YOUR_ARR as $val){
    $currCountry = $val["Country"];
    $currLive    = $val["Live"];

    if(!isset($subArrOfLive[$currCountry])) {
        $subArrOfLive[$currCountry] = $currLive;
    }else{
        $subArrOfLive[$currCountry] += $currLive;
    }

}

foreach($subArrOfLive as $key => $val){
    $temp = array(
        "Country" => $key,
        "provider" => "All Providers",
        "status" => "active",
        "# per status" => NULL,
        "Total Live" => $val
    );
    $YOUR_ARR[] = $temp ;
}