完善curl命令的输出

时间:2018-07-18 17:21:10

标签: shell curl

我有一个curl命令的输出,如下所示,

命令:curl https://application.com/api/projectcreator

输出:

{"Table":[{"key":"projectA","name":"Jhon"},
          {"key":"projectB","name":"Sam"},
          {"key":"ProjectC","name":"Jack"}]}

我想剪切此输出以仅获取名称。有没有办法在外壳中做到这一点?

例如: 俊 山姆 杰克

我在下面尝试过,但似乎对我来说并不算什么。

for Table in `curl -s -k http://application.com/api/projectcreator grep "name"`
do
   echo "$Table"
done

感谢您的帮助和见解!

1 个答案:

答案 0 :(得分:0)

使用4-digit,您可以轻松地做到这一点:

public class test
{
    public string[] content ={
        "Username:Peter",
        "ID:1",
        "Username:Nike",
        "ID:2"};

    public Dictionary<string, string> contents;

    public Dictionary<string, string> Contents
    {
        get
        {
            if (contents == null)
            {
                InitContents();
            }

            return contents;
        }
    }

    private void InitContents()
    {
        contents = new Dictionary<string, string>();

        for (var i = 0; i < content.Length; i++)
        {
            contents.Add(GetValue(content[i]), GetValue(content[i + 1]));
            i++;
        }
    }

    private string GetValue(string data)
    {
        return data.Substring(data.IndexOf(":", StringComparison.Ordinal) + 1);
    }

    public IEnumerable<string> GetContents()
    {
        return Contents.Select(x => "Name: " + x.Key + " ID:" + x.Value);
    }


}

treeView1.Nodes.AddRange(yourTests.Contents.Select(x=>new TreeNode("Name:"+x.Key+"Id:"+x.Value))))

如果无法安装jq,请使用curl -s -k 'http://application.com/api/projectcreator' | jq -r '.Table[].name' | paste -s -d ' '

Jhon Sam Jack