【发布时间】: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