PUT操作以错误的方式保存数据JSON WPF

时间:2014-07-09 06:13:49

标签: c# wpf json json.net

我尝试通过将一组JSON数据从WPF应用程序传递到服务器来执行PUT操作,并且我使用Newtonsoft来执行此操作。 Newtonsoft JObject为我提供了正确的JSON格式和所需数据,PUT操作也很成功,但更新数据后我可以看到更改没有完全反映在Server上 我用来生成结构的JSON的代码就像这个http://pastebin.com/hQPMm79H

JArray book_pref_val_root = new JArray();

JObject val_item;
JObject val_set;
JArray book_array;
string bookid = "";

//start of item 1
val_item = new JObject();

bookid = "52b020fda47ab2e07f000003";
val_item.Add(new JProperty("id", bookid));

book_array = new JArray();

val_set = new JObject();
val_set.Add(new JProperty("name", "key1"));
val_set.Add(new JProperty("value", 1));
book_array.Add(val_set);

val_set = new JObject();
val_set.Add(new JProperty("name", "key2"));
val_set.Add(new JProperty("value", 0));
book_array.Add(val_set);

val_set = new JObject();
val_set.Add(new JProperty("name", "key3"));
val_set.Add(new JProperty("value", 0.5));
book_array.Add(val_set);

val_item.Add(new JProperty("params", book_array));
//END of item 1

book_pref_val_root.Add(val_item);

//satrt of itm2
val_item = new JObject();

bookid = "522efc12cfe1c4a56e000005";
val_item.Add(new JProperty("id", bookid));

book_array = new JArray();

val_set = new JObject();
val_set.Add(new JProperty("name", "key1"));
val_set.Add(new JProperty("value", 1));
book_array.Add(val_set);

val_set = new JObject();
val_set.Add(new JProperty("name", "Key2"));
val_set.Add(new JProperty("value", .4));
book_array.Add(val_set);

val_item.Add(new JProperty("params", book_array));

//end of itm2

book_pref_val_root.Add(val_item);


JArray pref_root = new JArray();

JObject pref_item = new JObject();
pref_item.Add(new JProperty("name", "books"));
pref_item.Add(new JProperty("value", book_pref_val_root));
pref_root.Add(pref_item);

pref_item = new JObject();
pref_item.Add(new JProperty("name", "language"));
pref_item.Add(new JProperty("value", "en_US"));
pref_root.Add(pref_item);

pref_item = new JObject();
pref_item.Add(new JProperty("name", "app_zoom"));
pref_item.Add(new JProperty("value", 1));
pref_root.Add(pref_item);

JObject root_object = new JObject();
root_object.Add(new JProperty("token", "sadssad"));
root_object.Add(new JProperty("preferences", pref_root));
string json_string = root_object.ToString();

输出JSON格式在这里

"token": "sadssad",
"preferences": [{
"name": "books",
"value": [{
    "id": "52b020fda47ab2e07f000003",
    "params": [{
        "name": "key1",
        "value": 1
    }, {
        "name": "key2",
        "value": 0
    }, {
        "name": "key3",
        "value": 0.5
    }]
}, {
    "id": "522efc12cfe1c4a56e000005",
    "params": [{
        "name": "key1",
        "value": 1
    }, {
        "name": "key2",
        "value": 0.4
    }]
}]
}, {
"name": "language",
"value": "en_US"
}, {
"name": "app_zoom",
"value": 1
}]

现在我使用此代码在服务器上执行PUT操作

 var httpWebRequest = (HttpWebRequest)WebRequest.Create(url_toPUT);
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "PUT";
            using (var streamWriter = new   StreamWriter(httpWebRequest.GetRequestStream()))
            {
                streamWriter.Write(json_string);
                streamWriter.Flush();
                streamWriter.Close();
                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                if (httpResponse.StatusCode == HttpStatusCode.Created)
                {
                    bool actionStatus = true; 
                }
            }

PUT操作也很成功。但问题是我在PUT操作后检查远程服务器上的数据我可以看到首选项保存为

"preferences":[{"name":"books","value":"[object Object],[object Object]"},{"name":"language","value":"en_US"},{"name":"app_zoom","value":"1"}]

这里可以看到books参数的值只是[object object]类型。 什么是PUT数据时出现问题?有什么建议吗?

0 个答案:

没有答案