解析失败 JSON

Failed to Parse JSON

我在我的 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" }  ]

我打印字符串的函数是

店主

@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 对象中放置了引号,删除这些引号应该会使它有效 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"
    }
]

这是一种非常容易出错的方法来创建 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>

这对我有用 而不是

  • (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 : "")