小编hub*_*ish的帖子

JObject.Parse与JsonConvert.DeserializeObject

JsonConvert.DeserializeObject和JObject.Parse有什么区别?据我所知,两者都是一个字符串,并在Json.NET库中.什么样的情况会使一个人比另一个人更方便,还是主要只是偏好?

作为参考,这里有一个例子,我用两者做同样的事情 - 解析一个Json字符串并返回一个Json属性的列表.

public ActionResult ReadJson()
{
    string countiesJson = "{'Everything':[{'county_name':null,'description':null,'feat_class':'Civil','feature_id':'36865',"
                    +"'fips_class':'H1','fips_county_cd':'1','full_county_name':null,'link_title':null,'url':'http://www.alachuacounty.us/','name':'Alachua County'"+ ",'primary_latitude':'29.7','primary_longitude':'-82.33','state_abbreviation':'FL','state_name':'Florida'},"+
                    "{'county_name':null,'description':null,"+ "'feat_class':'Civil','feature_id':'36866','fips_class':'H1','fips_county_cd':'3','full_county_name':null,'link_title':null,'url':'http://www.bakercountyfl.org/','name':'Baker County','primary_latitude':'30.33','primary_longitude':'-82.29','state_abbreviation':'FL','state_name':'Florida'}]}";

    //Can use either JSONParseObject or JSONParseDynamic here
    List<string> counties = JSONParseObject(countiesJson);
    JSONParseDynamic(countiesJson);
    return View(counties);
}

public List<string> JSONParseObject(string jsonText)
{
    JObject jResults = JObject.Parse(jsonText);
    List<string> counties = new List<string>();
    foreach (var county in jResults["Everything"])
    {
        counties.Add((string)county["name"]);
    }
    return counties;
}

public List<string> JSONParseDynamic(string jsonText)
{
    dynamic jResults = JsonConvert.DeserializeObject(jsonText);
    List<string> counties = new List<string>();
    foreach(var county in jResults.Everything)
    {
        counties.Add((string)county.name);
    }
    return …
Run Code Online (Sandbox Code Playgroud)

c# jsonserializer json.net

73
推荐指数
2
解决办法
5万
查看次数

使用JSON.NET返回ActionResult

我正在尝试编写一个C#方法,它将序列化一个模型并返回一个JSON结果.这是我的代码:

    public ActionResult Read([DataSourceRequest] DataSourceRequest request)
    {
        var items = db.Words.Take(1).ToList();
        JsonSerializerSettings jsSettings = new JsonSerializerSettings();
        jsSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
        var converted = JsonConvert.SerializeObject(items, null, jsSettings);
        return Json(converted, JsonRequestBehavior.AllowGet);
    }
Run Code Online (Sandbox Code Playgroud)

当我转到Chrome中的Words/Read时,我得到了以下JSON结果:

"[{\"WordId\":1,\"Rank\":1,\"PartOfSpeech\":\"article\",\"Image\":\"Upload/29/1/Capture1.PNG\",\"FrequencyNumber\":\"22038615\",\"Article\":null,\"ClarificationText\":null,\"WordName\":\"the | article\",\"MasterId\":0,\"SoundFileUrl\":\"/UploadSound/7fd752a6-97ef-4a99-b324-a160295b8ac4/1/sixty_vocab_click_button.mp3\",\"LangId\":1,\"CatId\":null,\"IsActive\":false}
Run Code Online (Sandbox Code Playgroud)

我认为\'转义引号是双重序列化对象时出现的问题.从其他问题: WCF JSON输出添加了不需要的引号和反斜杠

它看起来好像是我对我的对象进行双重序列化,因为我首先使用JSON.NET进行序列化,然后将我的结果传递给Json()函数.我需要手动序列化以避免引用循环,但我认为我的View需要一个ActionResult.

我怎样才能在这里返回一个ActionResult?我需要,还是只能返回一个字符串?

c# serialization json.net asp.net-mvc-5

50
推荐指数
2
解决办法
6万
查看次数

为什么Git在运行"git checkout origin/<branch>"后告诉我"当前不在任何分支上"?

我试图按照Git的说明:"目前不在任何分支上." 有没有一种简单的方法可以回到分支上,同时保持变化?git checkout似乎被打破了:

$ git checkout origin/web-zach
HEAD is now at 1366cb1... Changed so css files not ignored

$ git status
# Not currently on any branch.
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       .cordova/config.xml
#       www/languages/pt/sounds/
nothing added to commit but untracked files present (use "git add" to track)
Run Code Online (Sandbox Code Playgroud)

更具体地说,我担心"目前没有任何分支"的消息.git checkout在这里似乎没有做任何事情......这个命令的全部目的不是把我放在树枝上吗?我怎样才能回到分支并再次提交/推送?

git remote-branch git-checkout

18
推荐指数
1
解决办法
3万
查看次数

猫鼬永远不会连接到mongodb

我正在尝试使用Amazon EC2 Linux服务器上的Mongoose连接到MongoDB.

这是我的代码:

var mongoose = require('mongoose');
console.log("Attempting antyhing to do with mongoose"); //shown

var db = mongoose.connection;
db.on('error',console.error.bind(console,'db connection error:')); //not shown
db.once('open',function(){
    console.log("Successful connection to db!"); //not shown
});

mongoose.connect('mongodb://localhost:27017/local',function(err){
    console.log("some kinda connection made"); //not shown
    if(err)
    {
        console.log("err: "+err);
    }
});
Run Code Online (Sandbox Code Playgroud)

令人沮丧的是,我没有从猫鼬中得到任何错误,但似乎没有任何错误出现.

关于mongoose和mongo没有回调似乎有很多问题.

这是我看过的一对夫妇,我觉得这对我来说不是问题所在:

另外参考我正在学习本教程:https://scotch.io/tutorials/build-a-restful-api-using-node-and-express-4

我担心的一件事是我将代码分成多个文件.所以这个mongoose连接代码是从app/models/host.js(或教程中的bear.js)文件中调用的.如果发布其他文件会有帮助,请告诉我.

amazon-ec2 mongoose mongodb node.js

6
推荐指数
1
解决办法
1704
查看次数

IAM服务帐户密钥与Google凭据文件

我正在编写代码以生成和下载Google Cloud服务帐户的私钥。

使用IAM API,我能够创建一个服务帐户,并且生成密钥的调用似乎正在工作。我会按照IAM API创建密钥页面所述获得服务帐户密钥,例如

{ 
  "privateKeyType": "TYPE_GOOGLE_CREDENTIALS_FILE",
  "privateKeyData": "random-key-stringkajdkjakjfke", ...
}
Run Code Online (Sandbox Code Playgroud)

我下载了此文件作为JSON响应,并尝试对其进行身份验证:

gcloud auth activate-service-account --key-file=service-account-key-file.json
Run Code Online (Sandbox Code Playgroud)

不幸的是,我收到一条错误消息 The .json key file is not in a valid format.

当我浏览Google Cloud Console流程(IAM和管理->服务帐户-> ...->创建密钥->创建)时,我得到了一个下载的JSON文件,看起来像

{
  "type": "service_account",
  "private_key": "----BEGIN-PRIVATE-KEY-----",
  "auth_uri": "https://gaiastaging.corp.google.com/o/oauth2/auth",
}
Run Code Online (Sandbox Code Playgroud)

该文件看起来与IAM API的响应完全不同。解释我的错误!不幸的是,这种格式似乎没有在任何地方描述。在某些文档中已简要提及。它是Google凭据文件吗?

我想获取IAM响应文件/ JSON,并将其转换为第二个凭证文件。我尝试编写一些代码来转换它,但是有些字段"auth_provider_x509_cert_url"我不理解。

也许转换文件也是错误的方法?更普遍:

如何生成文件,然后使用它对gcloud进行身份验证?

我应该如何描述/区分以上两个文件?为什么每种类型的文件都有用?

google-api google-cloud-platform google-iam

4
推荐指数
1
解决办法
928
查看次数

C++无效使用成员函数(你忘了'()'吗?)

我无法编译程序.错误发生在第165-177行.我添加的只是对字母存在的测试,我收到了错误,希望你能帮忙!完整代码http://pastebin.com/embed.php?i=WHrSasYk

(附在下面是代码)

do
{
  cout << "\nCustomer Details:";

  cout << "\n\tCustomer Name:";
  cout << "\n\t\tFirst Name:";
  getline (cin, Cust_FName, '\n');
  if (Quotation::Cust_FName.length() <= 1)
    ValidCustDetails = false;
  else
  {
    // Error line 165!
    for (unsigned short i = 0; i <= Cust_FName.length; i++)
      if (!isalpha(Quotation::Cust_FName.at(i)))
        ValidCustDetails = false;
  }
  cin.ignore();
  cout << "\t\tLast Name:";
  getline (cin, Cust_LName, '\n');
  if (Cust_LName.length () <= 1)
    ValidCustDetails = false;
  else
  {
    // Error line 177!
    for (unsigned short i = 0; i …
Run Code Online (Sandbox Code Playgroud)

c++ containers compiler-errors members

2
推荐指数
1
解决办法
2万
查看次数