简体   繁体   English

Json对象解析错误

[英]Json Object parse error

I am storing data in my cassandra database as string and i want to retrieve the string and convert it into json. 我将数据存储在我的cassandra数据库中作为字符串,我想检索该字符串并将其转换为json。 i get an exception saying 我得到一个例外的说法

An unhandled exception of type 'Newtonsoft.Json.JsonReaderException' happened 发生类型'Newtonsoft.Json.JsonReaderException'的未处理异常

is there something wrong with the data stored ? 存储的数据有问题吗? or i am doing some other thing wrong ? 还是我做错了其他事情?

My query is : 我的查询是:

INSERT INTO table2(key1,col1) values ('5',{'url':'{"hello":
    {"hi":{"hey":"1","time":"5"}},
    {"reg":{"hey":"1","time":"1"}},
    {"install":{"hey":"0"}}},
    "task":"1","retry":"00",
    "max":"5","call":"140"'});


In my db when i click the map<text,text> column, it gets stored as : 

 {\"hello\":\r\n    
    {\"subscription_atrisk\":{\"hey\":\"1\",\"time\":\"100\"}},
    {\"reg\":{\"hey\":\"1\",\"time\":\"2000\"}},
\"task\":\"0\",\"retry\":\"300\",\"max\":\"5\",\"call\":\"14400\"}

in my c# code, i try 在我的C#代码中,我尝试

string s = "{\"hello\":\r\n    {\"subscription_atrisk\":{\"hey\":\"1\",\"time\":\"100\"}},{\"reg\":{\"hey\":\"1\",\"time\":\"2000\"}},\"task\":\"0\",\"retry\":\"300\",\"max\":\"5\",\"call\":\"14400\"";
Jobject json = Jobject.Parse(s); //Get an Error here.

Could anyone please throw some light on this ? 任何人都可以对此有所了解吗?

似乎您缺少结束语}尝试:

string s = "{\"hello\": {\"subscription_atrisk\":{\"hey\":\"1\",\"time\":\"100\"}}, \"missing_key\": {\"reg\":{\"hey\":\"1\",\"time\":\"2000\"}},\"task\":\"0\",\"retry\":\"300\",\"max\":\"5\",\"call\":\"14400\"}";

In JSON, an object should contain a key and either a value or another object/array . 在JSON中,对象应包含一个键以及一个或另一个对象/数组 You have syntax errors in your JSON object. JSON对象中有语法错误。 First of all, always use double quotes. 首先,请始终使用双引号。

Then... The url object has a key Hello but then where you are supposed to place two more keys, you're just putting there two more objects, as if it was an array. 然后... url对象具有一个键Hello但是应该在其中再放置两个键,您只是在其中放置了两个对象,就好像它是一个数组一样。

This could be a correct syntax: 这可能是正确的语法:

{
"url": {
    "hello": {
        "hi": {
            "hey": "1",
            "time": "5"
        }
    },
    "MissingKey1":{
        "reg": {
            "hey": "1",
            "time": "1"
        }
    },
    "MissingKey2":{
        "install": {
            "hey": "0"
        }
    }
},
"task": "1",
"retry": "00",
"max": "5",
"call": "140"
}

Or if you really meant to have a hello object and an array of two more objects inside url : 或者,如果您真的要在url包含一个hello对象和另外两个对象的数组:

{
"url": {
    "hello": {
        "hi": {
            "hey": "1",
            "time": "5"
        }
    },
    "AnArray": [{
        "reg": {
            "hey": "1",
            "time": "1"
        }
    }, {
        "install": {
            "hey": "0"
        }
    }]
},
"task": "1",
"retry": "00",
"max": "5",
"call": "140"

}

I suggest that before you store those JSON objects in the database, ALWAYS validate them to make sure that they're valid of syntax. 我建议在将这些JSON对象存储在数据库中之前,请始终对其进行验证,以确保它们在语法上是有效的。

Or even better: this Newtonsoft library provides functions which serializes(creates) JSON strings from other objects. 甚至更好:这个Newtonsoft库提供了从其他对象序列化(创建)JSON字符串的函数。 See JsonConvert.SerializeObject Method 请参见JsonConvert.SerializeObject方法

In general it is a good idea to look around in the API documentation, it really can save you a lot of time so that you don't have to do unnecessary work. 通常,在API文档中浏览是个好主意,它确实可以节省很多时间,因此您不必进行不必要的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM