【问题标题】:Failed to Parse JSON [closed]无法解析 JSON [关闭]
【发布时间】:2021-09-19 06:47:40
【问题描述】:

我在我的 JS 代码上解析 JSON 失败,当我尝试在控制台上打印它并在在线 JSON 美化器上美化它时。它给了我这个错误

Parse error on line 1:
...-06-05 11:21:48.0"} ","productSellCatego
-----------------------^
Expecting 'EOF', '}', ',', ']', got 'STRING'

JSON

[{"id":"1" ,"storeName":"MyStore" ,"uniqueStoreCode":"MytURz718" ,"storeDescription":"how about this store" ,"storeStatus":"0" ,"gstinNumber":"mFnbkYGU5BdmijB0ZSp9Xw==" ,"sellerPanNumber":"MNBVK8520U" ,"storeAccountNumber":"Lf4HJliBJzWXIcYUW2CD7Q==" ,"country":"India" ,"state":"Uttar" ,"city":"Ghaziabad" ,"address":"b-1/136 s-1" ,"storeAccountHolderName":"Ramesh Sharma" ,"bankAccountType":"0" ,"addedAt":"2021-08-12 09:40:37.0" ,"storeOwner":{"id":"2", "uniqueUserId":"sec7M3dkH", "firstName":"Ramesh", "lastName":"Sharma", "dateOfBirth":"1979-07-23", "email":"sharmaascent6@gmail.com", "userName":"seller", "status":"0", "role":"1", "country":"null", "state":"null", "city":"null", "zipCode":"null", "address":"null", "gender":"0", "phoneNumber":"+919811708085", "addedAt":"2021-06-05 11:21:48.0"} ","productSellCategory":{"productCatId":"6", "categoryName":"Clothing and Accessories"}" ,"addedAt":"2021-08-12 09:40:37.0" } , {"id":"2" ,"storeName":"MyStore" ,"uniqueStoreCode":"My1mQfYDF" ,"storeDescription":"how about this store" ,"storeStatus":"0" ,"gstinNumber":"h/GcxLRmRljFa0nPE7GtRg==" ,"sellerPanNumber":"MNBVK8520U" ,"storeAccountNumber":"PkEBwQB/eiLktZBe8tDGPw==" ,"country":"India" ,"state":"Uttar" ,"city":"Ghaziabad" ,"address":"b-1/136 s-1" ,"storeAccountHolderName":"Ramesh Sharma" ,"bankAccountType":"0" ,"addedAt":"2021-08-12 09:43:02.0" ,"storeOwner":{"id":"2", "uniqueUserId":"sec7M3dkH", "firstName":"Ramesh", "lastName":"Sharma", "dateOfBirth":"1979-07-23", "email":"sharmaascent6@gmail.com", "userName":"seller", "status":"0", "role":"1", "country":"null", "state":"null", "city":"null", "zipCode":"null", "address":"null", "gender":"0", "phoneNumber":"+919811708085", "addedAt":"2021-06-05 11:21:48.0"} ","productSellCategory":"{"productCatId":"6", "categoryName":"Clothing and Accessories"}","addedAt":"2021-08-12 09:43:02.0" }    , {"id":"3" ,"storeName":"Sharma Ji Store" ,"uniqueStoreCode":"Shlv1BXrS" ,"storeDescription":"This is a beauty products store" ,"storeStatus":"0" ,"gstinNumber":"njauEy1GZ+qEkLt02av/Fw==" ,"sellerPanNumber":"MNBVK8520U" ,"storeAccountNumber":"oc6ZgAfuufHkOt0fyO/lNQ==" ,"country":"India" ,"state":"Delhi" ,"city":"East" ,"address":"gali no.16 adarsh mohhala" ,"storeAccountHolderName":"Ramesh Sharma" ,"bankAccountType":"0" ,"addedAt":"2021-08-12 09:45:58.0" ,"storeOwner":{"id":"2", "uniqueUserId":"sec7M3dkH", "firstName":"Ramesh", "lastName":"Sharma", "dateOfBirth":"1979-07-23", "email":"sharmaascent6@gmail.com", "userName":"seller", "status":"0", "role":"1", "country":"null", "state":"null", "city":"null", "zipCode":"null", "address":"null", "gender":"0", "phoneNumber":"+919811708085", "addedAt":"2021-06-05 11:21:48.0"} ","productSellCategory":{"productCatId":"3", "categoryName":"Beauty"} ","addedAt":"2021-08-12 09:45:58.0" }  ]

我打印字符串的函数是

storeOwner

@Override
    public String toString() {
        return "{\"id\":\"" + id + "\", \"uniqueUserId\":\"" + uniqueUserId + "\", \"firstName\":\"" + firstName
                + "\", \"lastName\":\"" + lastName + "\", \"dateOfBirth\":\"" + dateOfBirth + "\", \"email\":\"" + email
                + "\", \"userName\":\"" + userName + "\", \"status\":\"" + status + "\", \"role\":\"" + role
                + "\", \"country\":\"" + country + "\", \"state\":\"" + state + "\", \"city\":\"" + city
                + "\", \"zipCode\":\"" + zipCode + "\", \"address\":\"" + address + "\", \"gender\":\"" + gender
                + "\", \"phoneNumber\":\"" + phoneNumber + "\", \"addedAt\":\"" + addedAt + "\"}";
    }

商店

@Override
    public String toString() {
        return "{\"" +(id != null ? "id\":\"" + id + "\" " : "")
                     +(storeName != null ? ",\"storeName\":\"" + storeName + "\" " : "")
                     +(uniqueStoreCode != null ? ",\"uniqueStoreCode\":\"" + uniqueStoreCode + "\" " : "")
                     +(storeDescription != null ? ",\"storeDescription\":\"" + storeDescription + "\" " : "")
                     +(storeStatus != null ? ",\"storeStatus\":\"" + storeStatus + "\" " : "")
                     +(gstinNumber != null ? ",\"gstinNumber\":\"" + gstinNumber + "\" " : "")
                     +(sellerPanNumber != null ? ",\"sellerPanNumber\":\"" + sellerPanNumber + "\" " : "")
                     +(storeAccountNumber != null ? ",\"storeAccountNumber\":\"" + storeAccountNumber + "\" " : "")
                     +(country != null ? ",\"country\":\"" + country + "\" " : "")
                     +(state != null ? ",\"state\":\"" + state + "\" " : "")
                     +(city != null ? ",\"city\":\"" + city + "\" " : "")
                     +(address != null ? ",\"address\":\"" + address + "\" " : "")
                     +(storeAccountHolderName != null ? ",\"storeAccountHolderName\":\"" + storeAccountHolderName + "\" " : "")
                     +(bankAccountType != null ? ",\"bankAccountType\":\"" + bankAccountType + "\" " : "")
                     +(addedAt != null ? ",\"addedAt\":\"" + addedAt + "\" " : "")
                     + (Hibernate.isInitialized(storeOwner) && storeOwner != null ? ",\"storeOwner\":" + storeOwner + " \"" : "")
                     + (Hibernate.isInitialized(productSellCategory) && productSellCategory != null ? ",\"productSellCategory\":" + productSellCategory + " \""  : "")
//                   (Hibernate.isInitialized(deliveryStatusSet) && deliveryStatusSet != null ? ", \"deliveryStatusSet\":" + deliveryStatusSet : "")
                     +(addedAt != null ? ",\"addedAt\":\"" + addedAt + "\" " : "")
                     + "}\t";
    }

产品类别

@Override
    public String toString() {
        return "{\"productCatId\":\"" + productCatId + "\", \"categoryName\":\"" + categoryName + "\"}";

//          return "{\"" +(productCatId != null ? "productCatId\":\"" + productCatId + "\" " : "")
//                   +(categoryName != null ? ",\"categoryName\":\"" + categoryName + "\" " : "")
//                   + "}\t";
    
    }

【问题讨论】:

  • 错误消息中的 ^ 准确地指出了 JSON 的问题所在。再清楚不过了。
  • 对于像这样的不平凡的 Object->JSON 转换,最好使用 JSON-Binding API,而不是手动进行。 baeldung.com/java-json-binding-api

标签: java json spring-boot tostring jsonparser


【解决方案1】:

在您的 json 对象中放置了引号,删除这些引号应该使其成为有效的 json 例如 将","productSellCategory":"{ 更改为, "productSellCategory": {

去掉引号后你的 json 是

[
    {
        "id": "1",
        "storeName": "MyStore",
        "uniqueStoreCode": "MytURz718",
        "storeDescription": "how about this store",
        "storeStatus": "0",
        "gstinNumber": "mFnbkYGU5BdmijB0ZSp9Xw==",
        "sellerPanNumber": "MNBVK8520U",
        "storeAccountNumber": "Lf4HJliBJzWXIcYUW2CD7Q==",
        "country": "India",
        "state": "Uttar",
        "city": "Ghaziabad",
        "address": "b-1/136 s-1",
        "storeAccountHolderName": "Ramesh Sharma",
        "bankAccountType": "0",
        "addedAt": "2021-08-12 09:40:37.0",
        "storeOwner": {
            "id": "2",
            "uniqueUserId": "sec7M3dkH",
            "firstName": "Ramesh",
            "lastName": "Sharma",
            "dateOfBirth": "1979-07-23",
            "email": "sharmaascent6@gmail.com",
            "userName": "seller",
            "status": "0",
            "role": "1",
            "country": "null",
            "state": "null",
            "city": "null",
            "zipCode": "null",
            "address": "null",
            "gender": "0",
            "phoneNumber": "+919811708085",
            "addedAt": "2021-06-05 11:21:48.0"
        },
        "productSellCategory": {
            "productCatId": "6",
            "categoryName": "Clothing and Accessories"
        },
        "addedAt": "2021-08-12 09:40:37.0"
    },
    {
        "id": "2",
        "storeName": "MyStore",
        "uniqueStoreCode": "My1mQfYDF",
        "storeDescription": "how about this store",
        "storeStatus": "0",
        "gstinNumber": "h/GcxLRmRljFa0nPE7GtRg==",
        "sellerPanNumber": "MNBVK8520U",
        "storeAccountNumber": "PkEBwQB/eiLktZBe8tDGPw==",
        "country": "India",
        "state": "Uttar",
        "city": "Ghaziabad",
        "address": "b-1/136 s-1",
        "storeAccountHolderName": "Ramesh Sharma",
        "bankAccountType": "0",
        "addedAt": "2021-08-12 09:43:02.0",
        "storeOwner": {
            "id": "2",
            "uniqueUserId": "sec7M3dkH",
            "firstName": "Ramesh",
            "lastName": "Sharma",
            "dateOfBirth": "1979-07-23",
            "email": "sharmaascent6@gmail.com",
            "userName": "seller",
            "status": "0",
            "role": "1",
            "country": "null",
            "state": "null",
            "city": "null",
            "zipCode": "null",
            "address": "null",
            "gender": "0",
            "phoneNumber": "+919811708085",
            "addedAt": "2021-06-05 11:21:48.0"
        },
        "productSellCategory": {
            "productCatId": "6",
            "categoryName": "Clothing and Accessories"
        },
        "addedAt": "2021-08-12 09: 43: 02.0"
    },
    {
        "id": "3",
        "storeName": "Sharma Ji Store",
        "uniqueStoreCode": "Shlv1BXrS",
        "storeDescription": "This is a beauty products store",
        "storeStatus": "0",
        "gstinNumber": "njauEy1GZ+qEkLt02av/Fw==",
        "sellerPanNumber": "MNBVK8520U",
        "storeAccountNumber": "oc6ZgAfuufHkOt0fyO/lNQ==",
        "country": "India",
        "state": "Delhi",
        "city": "East",
        "address": "gali no.16 adarsh mohhala",
        "storeAccountHolderName": "Ramesh Sharma",
        "bankAccountType": "0",
        "addedAt": "2021-08-12 09: 45: 58.0",
        "storeOwner": {
            "id": "2",
            "uniqueUserId": "sec7M3dkH",
            "firstName": "Ramesh",
            "lastName": "Sharma",
            "dateOfBirth": "1979-07-23",
            "email": "sharmaascent6@gmail.com",
            "userName": "seller",
            "status": "0",
            "role": "1",
            "country": "null",
            "state": "null",
            "city": "null",
            "zipCode": "null",
            "address": "null",
            "gender": "0",
            "phoneNumber": "+919811708085",
            "addedAt": "2021-06-05 11: 21: 48.0"
        },
        "productSellCategory": {
            "productCatId": "3",
            "categoryName": "Beauty"
        },
        "addedAt": "2021-08-12 09: 45: 58.0"
    }
]

【讨论】:

  • 感谢您的帮助,请您澄清 ","productSellCategory":"{ AND ","productSellCategory":"{ 之间的区别,因为它们看起来很相似
  • @shivamsharma,我已经更正了我的回答中的错字,请检查
【解决方案2】:

这是创建 JSON 的一种非常容易出错的方法。

相反,创建一个 POJO 并使用 Gson 或 Jackson 将其转换为 JSON 字符串:

import com.google.gson.Gson;

public class SO69240970 {
    static class MyData {
        String a;
        String b;
        int c;
        public MyData(String a, String b, int c) {
            this.a = a;
            this.b = b;
            this.c = c;
        }
    }

    public static void main(String[] args) {
        MyData m = new MyData("A value", "B \"value\"", 102);
        System.out.println(new Gson().toJson(m));
    }
}

输出:{"a":"A value","b":"B \"value\"","c":102}

Maven 依赖:

    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.8</version>
    </dependency>

【讨论】:

  • 谢谢,我一定会试一试的。
【解决方案3】:

这对我有用 而不是

  • (Hibernate.isInitialized(storeOwner) && storeOwner != null ? ","storeOwner":" + storeOwner + " "" : "") + (Hibernate.isInitialized(productSellCategory) && productSellCategory != null ? ","productSellCategory":" + productSellCategory + " "" : "")

我用过

  • (Hibernate.isInitialized(storeOwner) && storeOwner != null ? ","storeOwner":" + storeOwner :"" ) + (Hibernate.isInitialized(productSellCategory) && productSellCategory != null ? ","productSellCategory":" + productSellCategory : "")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多