Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么Jansson';s_json_object()是否无法识别我的json字符串? 我是C++的新手,无法计算如何从字符串中删除一些杂项数据,然后将其解析为JSON._C++_Json_Jansson - Fatal编程技术网

为什么Jansson';s_json_object()是否无法识别我的json字符串? 我是C++的新手,无法计算如何从字符串中删除一些杂项数据,然后将其解析为JSON.

为什么Jansson';s_json_object()是否无法识别我的json字符串? 我是C++的新手,无法计算如何从字符串中删除一些杂项数据,然后将其解析为JSON.,c++,json,jansson,C++,Json,Jansson,我最终使用了我能找到的文档最多的JSON解析器——jansson。看起来很棒,虽然我在第一个栏上被卡住了 我的程序接收以下格式的字符串: 5::/chat:{"name":"steve","args":[{"connection":"true"}, { "chatbody" : "I am the body" }]} 我用以下方法去除了花括号外的所有内容: std::string str=message; unsigned pos = str.find("{"); std::string st

我最终使用了我能找到的文档最多的JSON解析器——jansson。看起来很棒,虽然我在第一个栏上被卡住了

我的程序接收以下格式的字符串:

5::/chat:{"name":"steve","args":[{"connection":"true"}, { "chatbody" : "I am the body" }]}
我用以下方法去除了花括号外的所有内容:

std::string str=message;
unsigned pos = str.find("{");
std::string string = str.substr (pos);
剩下的是:

{
    "name": "steve",
    "args": [
        {
            "connection": "true"
        },
        {
            "chatbody": "I am the body"
        }
    ]
}
我被困在第一阶段分析这个。我已将字符串转换为字符,然后尝试使用json_加载,但没有得到任何有用的结果

整个事情看起来是这样的:

void processJson(string message)
{
    json_t *root;
    json_error_t error;
    size_t i;

    std::string str=message;
    unsigned pos = str.find("{");
    std::string str3 = str.substr (pos);

    const char * c = str.c_str();

    json_t *data, *sha, *name;

    root = json_loads(c, 0, &error);
    data = json_array_get(root, i);        
    cout << data;

    if(!json_is_object(root))
    {
      fprintf(stderr, "error: commit data %d is not an object\n", i + 1);
    }

}
我做错了什么?如何正确格式化?最终,我需要迭代一个数组,但无法通过。我相信这只是初学者的错误

-编辑-


试图避免使用Boost,因为它有严格的大小要求。

我不熟悉您使用的JSON库,但这里有一些关于可能错误的建议

  • size\u ti
    没有初始化为任何内容,然后被传递到
    json\u array\u get
  • json\u array\u get
    传递根对象,它不是一个json数组,而是一个json对象。在JSON术语中,
    root[“args”]
    将是数组

当然,根据JSON库的语义,这两个问题可能都不是问题,但它们对我来说似乎是危险信号。

您可以始终使用现有的解决方案,如Boost的属性树,它具有自动解析JSON文件的功能。实际上,添加这两个标题非常简单:

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
如果您想从JSON树中提取信息,可以这样做,其中type是要提取的数据类型,insert.key.path.here是密钥的路径,每个父密钥用句点分隔

jsonfile.get<type>(insert.key.path.here);
您可以在这里检查JSON字符串的有效性:

有一个非常好的JSON解析器,即使您不使用HTTP功能,也可以使用它

您可以将JSON解析器文件提取到静态库中,并将其与现有项目链接。要提取的文件包括:

src\json\json.cpp
src\json\json_parsing.cpp
src\json\json_serialization.cpp
src\utilities\asyncrt_utils.cpp

include\cpprest\json.h
include\cpprest\xxpublic.h
include\cpprest\basic_types.h
include\cpprest\asyncrt_utils.h
我可以确认这是有效的,因为我最近使用它作为我项目的静态库


我也尝试使用彭图斯·扬松,但是Casablanca的解析器更容易使用,并且有更好的Unicode支持。

< > JSON格式,我已经通过C++搜索了一个漂亮的打印解决方案。最后,我找到了一些java代码,我最终把它们转换成C++。请尝试以下JSON格式:

std::string formattedJson(char *json)
{
    std::string pretty;

    if (json == NULL || strlen(json) == 0)
    {
        return pretty;
    }

    std::string str     = std::string(json);
    bool        quoted  = false;
    bool        escaped = false;
    std::string INDENT  = "    ";
    int         indent  = 0;
    int         length  = (int) str.length();
    int         i;

    for (i = 0 ; i < length ; i++)
    {
        char ch = str[i];

        switch (ch)
        {
            case '{':
            case '[':
                pretty += ch;

                if (!quoted)
                {
                    pretty += "\n";

                    if (!(str[i+1] == '}' || str[i+1] == ']'))
                    {
                        ++indent;

                        for (int j = 0 ; j < indent ; j++)
                        {
                            pretty += INDENT;
                        }
                    }
                }

                break;

            case '}':
            case ']':
                if (!quoted)
                {
                    if ((i > 0) && (!(str[i-1] == '{' || str[i-1] == '[')))
                    {
                        pretty += "\n";

                        --indent;

                        for (int j = 0 ; j < indent ; j++)
                        {
                            pretty += INDENT;
                        }
                    }
                    else if ((i > 0) && ((str[i-1] == '[' && ch == ']') || (str[i-1] == '{' && ch == '}')))
                    {
                        for (int j = 0 ; j < indent ; j++)
                        {
                            pretty += INDENT;
                        }
                    }
                }

                pretty += ch;

                break;

            case '"':
                pretty += ch;
                escaped = false;

                if (i > 0 && str[i-1] == '\\')
                {
                    escaped = !escaped;
                }

                if (!escaped)
                {
                    quoted = !quoted;
                }

                break;

            case ',':
                pretty += ch;

                if (!quoted)
                {
                    pretty += "\n";

                    for (int j = 0 ; j < indent ; j++)
                    {
                        pretty += INDENT;
                    }
                }

                break;

            case ':':
                pretty += ch;

                if (!quoted)
                {
                    pretty += " ";
                }

                break;

            default:
                pretty += ch;

                break;
        }
    }

    return pretty;
}
std::string formattedJson(char*json)
{
字符串漂亮;
if(json==NULL | | strlen(json)==0)
{
回归美丽;
}
std::string str=std::string(json);
bool-quoted=false;
布尔逃逸=假;
std::字符串缩进=”;
int缩进=0;
int length=(int)str.length();
int i;
对于(i=0;i0)和(!(str[i-1]='{'| | str[i-1]=='['))
{
漂亮+=“\n”;
--缩进;
对于(int j=0;j0)和((str[i-1]='['&&ch=']'))| |(str[i-1]=='{'&&ch='}'))
{
对于(int j=0;j0&&str[i-1]=='\\')
{
逃脱=!逃脱;
}
如果(!转义)
{
quoted=!quoted;
}
打破
案例',':
漂亮+=ch;
如果(!引用)
{
漂亮+=“\n”;
对于(int j=0;j
它不应该是
const char*c=str3.c_str();
错误告诉你什么?(你必须检查它,对吗?)这不完全是一个错误,这是控制台输出,因为它不是json对象。@ShashwatKumar Sheesh,你有鹰眼。是的,没错。现在它说的是根是一个对象,但不是字符串。那么,这是什么呢???我要避免使用boost库,因为我空间和内存不足-需要在一个32Mb RAM的嵌入式设备上运行它。.他我试过Lijson、Raijdson、JSONCPP和JANSON。对于新来的C++来说,它们都太复杂了。
"connection" : true,
src\json\json.cpp
src\json\json_parsing.cpp
src\json\json_serialization.cpp
src\utilities\asyncrt_utils.cpp

include\cpprest\json.h
include\cpprest\xxpublic.h
include\cpprest\basic_types.h
include\cpprest\asyncrt_utils.h
std::string formattedJson(char *json)
{
    std::string pretty;

    if (json == NULL || strlen(json) == 0)
    {
        return pretty;
    }

    std::string str     = std::string(json);
    bool        quoted  = false;
    bool        escaped = false;
    std::string INDENT  = "    ";
    int         indent  = 0;
    int         length  = (int) str.length();
    int         i;

    for (i = 0 ; i < length ; i++)
    {
        char ch = str[i];

        switch (ch)
        {
            case '{':
            case '[':
                pretty += ch;

                if (!quoted)
                {
                    pretty += "\n";

                    if (!(str[i+1] == '}' || str[i+1] == ']'))
                    {
                        ++indent;

                        for (int j = 0 ; j < indent ; j++)
                        {
                            pretty += INDENT;
                        }
                    }
                }

                break;

            case '}':
            case ']':
                if (!quoted)
                {
                    if ((i > 0) && (!(str[i-1] == '{' || str[i-1] == '[')))
                    {
                        pretty += "\n";

                        --indent;

                        for (int j = 0 ; j < indent ; j++)
                        {
                            pretty += INDENT;
                        }
                    }
                    else if ((i > 0) && ((str[i-1] == '[' && ch == ']') || (str[i-1] == '{' && ch == '}')))
                    {
                        for (int j = 0 ; j < indent ; j++)
                        {
                            pretty += INDENT;
                        }
                    }
                }

                pretty += ch;

                break;

            case '"':
                pretty += ch;
                escaped = false;

                if (i > 0 && str[i-1] == '\\')
                {
                    escaped = !escaped;
                }

                if (!escaped)
                {
                    quoted = !quoted;
                }

                break;

            case ',':
                pretty += ch;

                if (!quoted)
                {
                    pretty += "\n";

                    for (int j = 0 ; j < indent ; j++)
                    {
                        pretty += INDENT;
                    }
                }

                break;

            case ':':
                pretty += ch;

                if (!quoted)
                {
                    pretty += " ";
                }

                break;

            default:
                pretty += ch;

                break;
        }
    }

    return pretty;
}