字符处的预期文字值 - Json字符串

时间:2014-08-06 13:12:52

标签: java android json

这是我的Json字符串:

[{\DoctorId\:3,\UserId\:0,\FirstName\:\Nitish\,\MiddleName\:\\,\LastName\:\Rana
\,\Qualification\:null,\Description\:null,\Experience\:null,\Email\:\d@d.com\,\Phone
\:\3245\,\AddressId\:66,\LocationId\:51},{\DoctorId\:4,\UserId\:0,\FirstName\:\Nitish
\,\MiddleName\:\\,\LastName\:\Rana\,\Qualification\:null,\Description\:null,\Experience
\:null,\Email\:\d@d.com\,\Phone\:\3245\,\AddressId\:67,\LocationId\:52},{\DoctorId
\:5,\UserId\:0,\FirstName\:\Nitish\,\MiddleName\:\\,\LastName\:\Rana\,\Qualification
\:null,\Description\:null,\Experience\:null,\Email\:\d@d.com\,\Phone\:\3245\,\AddressId
\:68,\LocationId\:53},{\DoctorId\:6,\UserId\:0,\FirstName\:\wefwe\,\MiddleName
\:\\,\LastName\:\rthyt\,\Qualification\:\\,\Description\:\\,\Experience\:\\,\Email
\:\w@w.com\,\Phone\:\43256\,\AddressId\:69,\LocationId\:54},{\DoctorId\:7,\UserId
\:0,\FirstName\:\dwefw\,\MiddleName\:\\,\LastName\:\tfhbede\,\Qualification
\:\\,\Description\:\\,\Experience\:\\,\Email\:\w@w.com\,\Phone\:\345\,\AddressId
\:70,\LocationId\:55},{\DoctorId\:8,\UserId\:0,\FirstName\:\ghnsfssw\,\MiddleName
\:\\,\LastName\:\fdvfd\,\Qualification\:\\,\Description\:\\,\Experience\:\\,\Emai
\:\w@w.com\,\Phone\:\3245\,\AddressId\:71,\LocationId\:56},{\DoctorId\:9,\UserId
\:0,\FirstName\:\csdcs\,\MiddleName\:\\,\LastName\:\dfvfgb\,\Qualification
\:\\,\Description\:\\,\Experience\:\\,\Email\:\q@a.com\,\Phone\:\34567\,\AddressId
\:72,\LocationId\:57},{\DoctorId\:10,\UserId\:0,\FirstName\:\regresrg\,\MiddleName
\:\\,\LastName\:\ukyt\,\Qualification\:\\,\Description\:\\,\Experience\:\\,\Email
\:\q@q.com\,\Phone\:\3242567\,\AddressId\:73,\LocationId\:58},{\DoctorId\:11,\UserId
\:0,\FirstName\:\fvger\,\MiddleName\:\\,\LastName\:\ujtyhr\,\Qualification
\:\\,\Description\:\\,\Experience\:\\,\Email\:\d@s.com\,\Phone\:\43567\,\AddressId
\:74,\LocationId\:59},{\DoctorId\:12,\UserId\:0,\FirstName\:\gfere\,\MiddleName
\:\\,\LastName\:\ergfe\,\Qualification\:\\,\Description\:\\,\Experience\:\\,\Email
\:\q@q.com\,\Phone\:\32465\,\AddressId\:75,\LocationId\:60},{\DoctorId\:13,\UserId
\:0,\FirstName\:\erfe\,\MiddleName\:\\,\LastName\:\rtghynu\,\Qualification
\:\\,\Description\:\\,\Experience\:\\,\Email\:\s@q.com\,\Phone\:\23456\,\AddressId
\:76,\LocationId\:61},{\DoctorId\:15,\UserId\:0,\FirstName\:\Nitish\,\MiddleName
\:\\,\LastName\:\Rana\,\Qualification\:null,\Description\:null,\Experience\:null,\Email
\:\d@d.com\,\Phone\:\3245\,\AddressId\:78,\LocationId\:63}]  

我在以下行中遇到异常:

JSONArray dd = new JSONArray(value);   // value = above json string  

例外是:

Expected literal value at character 2 of value.  

4 个答案:

答案 0 :(得分:3)

根据json.org(必须有资格作为可信/官方来源),JSON string(在您的JSON array中使用,作为名称和值),必须使用双引号引用

  

string 是零个或多个Unicode字符的序列,用双引号括起来,使用反斜杠转义符。

也许你或其他人误读这意味着你应该使用反斜杠转义而不是双引号?

它的真正含义是您必须在"周围使用双引号(string)。如果要使用十六进制形式的双引号,反斜杠,控制字符或unicode字符,则反斜杠字符仅用作引用字符串中的转义字符,作为您的一部分string

您可以在Ecma 404 standard document中阅读有关JSON格式的更多信息,但我怀疑您需要的不仅仅是json.org中的语法和示例。

在任何情况下,根据JSON格式,您的输入无效。您获得的异常是正确的,并且是此输入的预期。

以下是您可读且正确的整个JSON数组:

[
    {
        "DoctorId": 3,
        "UserId": 0,
        "FirstName": "Nitish",
        "MiddleName": "",
        "LastName": "Rana",
        "Qualification": null,
        "Description": null,
        "Experience": null,
        "Email": "d@d.com",
        "Phone": "3245",
        "AddressId": 66,
        "LocationId": 51
    },
    {
        "DoctorId": 4,
        "UserId": 0,
        "FirstName": "Nitish ",
        "MiddleName": "",
        "LastName": "Rana",
        "Qualification": null,
        "Description": null,
        "Experience ": null,
        "Email": "d@d.com",
        "Phone": "3245",
        "AddressId": 67,
        "LocationId": 52
    },
    {
        "DoctorId ": 5,
        "UserId": 0,
        "FirstName": "Nitish",
        "MiddleName": "",
        "LastName": "Rana",
        "Qualification ": null,
        "Description": null,
        "Experience": null,
        "Email": "d@d.com",
        "Phone": "3245",
        "AddressId ": 68,
        "LocationId": 53
    },
    {
        "DoctorId": 6,
        "UserId": 0,
        "FirstName": "wefwe",
        "MiddleName ": "",
        "LastName": "rthyt",
        "Qualification": "",
        "Description": "",
        "Experience": "",
        "Email ": "w@w.com",
        "Phone": "43256",
        "AddressId": 69,
        "LocationId": 54
    },
    {
        "DoctorId": 7,
        "UserId ": 0,
        "FirstName": "dwefw",
        "MiddleName": "",
        "LastName": "tfhbede",
        "Qualification ": "",
        "Description": "",
        "Experience": "",
        "Email": "w@w.com",
        "Phone": "345",
        "AddressId ": 70,
        "LocationId": 55
    },
    {
        "DoctorId": 8,
        "UserId": 0,
        "FirstName": "ghnsfssw",
        "MiddleName ": "",
        "LastName": "fdvfd",
        "Qualification": "",
        "Description": "",
        "Experience": "",
        "Emai ": "w@w.com",
        "Phone": "3245",
        "AddressId": 71,
        "LocationId": 56
    },
    {
        "DoctorId": 9,
        "UserId ": 0,
        "FirstName": "csdcs",
        "MiddleName": "",
        "LastName": "dfvfgb",
        "Qualification ": "",
        "Description": "",
        "Experience": "",
        "Email": "q@a.com",
        "Phone": "34567",
        "AddressId ": 72,
        "LocationId": 57
    },
    {
        "DoctorId": 10,
        "UserId": 0,
        "FirstName": "regresrg",
        "MiddleName ": "",
        "LastName": "ukyt",
        "Qualification": "",
        "Description": "",
        "Experience": "",
        "Email ": "q@q.com",
        "Phone": "3242567",
        "AddressId": 73,
        "LocationId": 58
    },
    {
        "DoctorId": 11,
        "UserId ": 0,
        "FirstName": "fvger",
        "MiddleName": "",
        "LastName": "ujtyhr",
        "Qualification ": "",
        "Description": "",
        "Experience": "",
        "Email": "d@s.com",
        "Phone": "43567",
        "AddressId ": 74,
        "LocationId": 59
    },
    {
        "DoctorId": 12,
        "UserId": 0,
        "FirstName": "gfere",
        "MiddleName ": "",
        "LastName": "ergfe",
        "Qualification": "",
        "Description": "",
        "Experience": "",
        "Email ": "q@q.com",
        "Phone": "32465",
        "AddressId": 75,
        "LocationId": 60
    },
    {
        "DoctorId": 13,
        "UserId ": 0,
        "FirstName": "erfe",
        "MiddleName": "",
        "LastName": "rtghynu",
        "Qualification ": "",
        "Description": "",
        "Experience": "",
        "Email": "s@q.com",
        "Phone": "23456",
        "AddressId ": 76,
        "LocationId": 61
    },
    {
        "DoctorId": 15,
        "UserId": 0,
        "FirstName": "Nitish",
        "MiddleName ": "",
        "LastName": "Rana",
        "Qualification": null,
        "Description": null,
        "Experience": null,
        "Email ": "d@d.com",
        "Phone": "3245",
        "AddressId": 78,
        "LocationId": 63
    }
]

正如许多其他人已经指出的那样,您可以使用正则表达式或类似方法轻松解决此问题,将所有\字符替换为"字符。

答案 1 :(得分:1)

您的JSON无效。我不知道你为什么用\或\\。用“或”替换那些。

更正JSON:

[
  {
    "DoctorId": 3,
    "UserId": 0,
    "FirstName": "Nitish",
    "MiddleName": "",
    "LastName": "Rana",
    "Qualification": null,
    "Description": null,
    "Experience": null,
    "Email": "d@d.com",
    "Phone": "3245",
    "AddressId": 66,
    "LocationId": 51
  },
  {
    "DoctorId": 4,
    "UserId": 0,
    "FirstName": "Nitish",
    "MiddleName": "",
    "LastName": "Rana",
    "Qualification": null,
    "Description": null,
    "Experience": null,
    "Email": "d@d.com",
    "Phone": "3245",
    "AddressId": 67,
    "LocationId": 52
  },
  {
    "DoctorId": 5,
    "UserId": 0,
    "FirstName": "Nitish",
    "MiddleName": "",
    "LastName": "Rana",
    "Qualification": null,
    "Description": null,
    "Experience": null,
    "Email": "d@d.com",
    "Phone": "3245",
    "AddressId": 68,
    "LocationId": 53
  },
  {
    "DoctorId": 6,
    "UserId": 0,
    "FirstName": "wefwe",
    "MiddleName": "",
    "LastName": "rthyt",
    "Qualification": "",
    "Description": "",
    "Experience": "",
    "Email": "w@w.com",
    "Phone": "43256",
    "AddressId": 69,
    "LocationId": 54
  },
  {
    "DoctorId": 7,
    "UserId": 0,
    "FirstName": "dwefw",
    "MiddleName": "",
    "LastName": "tfhbede",
    "Qualification": "",
    "Description": "",
    "Experience": "",
    "Email": "w@w.com",
    "Phone": "345",
    "AddressId": 70,
    "LocationId": 55
  },
  {
    "DoctorId": 8,
    "UserId": 0,
    "FirstName": "ghnsfssw",
    "MiddleName": "",
    "LastName": "fdvfd",
    "Qualification": "",
    "Description": "",
    "Experience": "",
    "Email": "w@w.com",
    "Phone": "3245",
    "AddressId": 71,
    "LocationId": 56
  },
  {
    "DoctorId": 9,
    "UserId": 0,
    "FirstName": "csdcs",
    "MiddleName": "",
    "LastName": "dfvfgb",
    "Qualification": "",
    "Description": "",
    "Experience": "",
    "Email": "q@a.com",
    "Phone": "34567",
    "AddressId": 72,
    "LocationId": 57
  },
  {
    "DoctorId": 10,
    "UserId": 0,
    "FirstName": "regresrg",
    "MiddleName": "",
    "LastName": "ukyt",
    "Qualification": "",
    "Description": "",
    "Experience": "",
    "Email": "q@q.com",
    "Phone": "3242567",
    "AddressId": 73,
    "LocationId": 58
  },
  {
    "DoctorId": 11,
    "UserId": 0,
    "FirstName": "fvger",
    "MiddleName": "",
    "LastName": "ujtyhr",
    "Qualification": "",
    "Description": "",
    "Experience": "",
    "Email": "d@s.com",
    "Phone": "43567",
    "AddressId": 74,
    "LocationId": 59
  },
  {
    "DoctorId": 12,
    "UserId": 0,
    "FirstName": "gfere",
    "MiddleName": "",
    "LastName": "ergfe",
    "Qualification": "",
    "Description": "",
    "Experience": "",
    "Email": "q@q.com",
    "Phone": "32465",
    "AddressId": 75,
    "LocationId": 60
  },
  {
    "DoctorId": 13,
    "UserId": 0,
    "FirstName": "erfe",
    "MiddleName": "",
    "LastName": "rtghynu",
    "Qualification": "",
    "Description": "",
    "Experience": "",
    "Email": "s@q.com",
    "Phone": "23456",
    "AddressId": 76,
    "LocationId": 61
  },
  {
    "DoctorId": 15,
    "UserId": 0,
    "FirstName": "Nitish",
    "MiddleName": "",
    "LastName": "Rana",
    "Qualification": null,
    "Description": null,
    "Experience": null,
    "Email": "d@d.com",
    "Phone": "3245",
    "AddressId": 78,
    "LocationId": 63
  }
]

您可以测试您的json是否正确。

网站:

  1. http://zaach.github.io/jsonlint/
  2. http://www.jsoneditoronline.org/

答案 2 :(得分:0)

你JSON看起来无效。

不确定源的来源,但String应该用双引号括起来,而不是反斜杠。

E.g。

[
   {
      "DoctorId":3,
      "UserId":0,
...

   }
]

答案 3 :(得分:0)

您的JSON无效。以下链接提供了有效JSON语法的示例。 Valid JSON请注意使用双引号。虽然在JavaScript中交替使用单引号和双引号是完全正确的,但单引号在JSON中无效。如果您已经完成了任何程度的Java编程,那么您必须在Strings中转义双引号字符这一事实应该是显而易见的。