Error while Parsing JSON to Object

Satish Dhiman

We have Json and we are try to parse json to object but we are getting exception code has explained below.

   {
    "ResultSet": {
        "Query": "Peabody",
        "Result": [
            {
                "symbol": "BTU",
                "name": "Peabody Energy Corporation",
                "exch": "NYQ",
                "type": "S",
                "exchDisp": "NYSE",
                "typeDisp": "Equity"
            },
            {
                "symbol": "BTU.MX",
                "name": "Peabody Energy Corporation",
                "exch": "MEX",
                "type": "S",
                "exchDisp": "Mexico",
                "typeDisp": "Equity"
            },
            {
                "symbol": "PE5.MU",
                "name": "PEABODY ENERGY",
                "exch": "MUN",
                "type": "S",
                "exchDisp": "Munich",
                "typeDisp": "Equity"
            },
            {
                "symbol": "PE5.F",
                "name": "PEABODY ENERGY",
                "exch": "FRA",
                "type": "S",
                "exchDisp": "Frankfurt",
                "typeDisp": "Equity"
            },
            {
                "symbol": "PE5.DU",
                "name": "PEABODY ENERGY",
                "exch": "DUS",
                "type": "S",
                "exchDisp": "Dusseldorf Stock Exchange",
                "typeDisp": "Equity"
            },
            {
                "symbol": "PE5.SG",
                "name": "PEABODY ENERGY",
                "exch": "STU",
                "type": "S",
                "exchDisp": "Stuttgart",
                "typeDisp": "Equity"
            },
            {
                "symbol": "PE51.F",
                "name": "PEABODY ENERGY",
                "exch": "FRA",
                "type": "S",
                "exchDisp": "Frankfurt",
                "typeDisp": "Equity"
            },
            {
                "symbol": "PE51.DU",
                "name": "PEABODY ENERGY",
                "exch": "DUS",
                "type": "S",
                "exchDisp": "Dusseldorf Stock Exchange",
                "typeDisp": "Equity"
            },
            {
                "symbol": "PE51.MU",
                "name": "PEABODY ENERGY",
                "exch": "MUN",
                "type": "S",
                "exchDisp": "Munich",
                "typeDisp": "Equity"
            },
            {
                "symbol": "PE51.SG",
                "name": "PEABODY ENERGY",
                "exch": "STU",
                "type": "S",
                "exchDisp": "Stuttgart",
                "typeDisp": "Equity"
            }
        ]
    }
}

And we have classes:-

class CustomJSON {
    private String ResultSet;
    private String Query;
    private List<Result> Result = new ArrayList<Result>();
    public String getResultSet() {
        return ResultSet;
    }
    public void setResultSet(String resultSet) {
        ResultSet = resultSet;
    }
    public String getQuery() {
        return Query;
    }
    public void setQuery(String query) {
        Query = query;
    }
    public List<Result> getResult() {
        return Result;
    }
    public void setResult(List<Result> result) {
        Result = result;
    }


}

class Result{

    private String symbol;
    private String name;
    private String exch;
    private String type;
    private String exchDisp;
    private String typeDisp;
    public String getSymbol() {
        return symbol;
    }
    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getExch() {
        return exch;
    }
    public void setExch(String exch) {
        this.exch = exch;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getExchDisp() {
        return exchDisp;
    }
    public void setExchDisp(String exchDisp) {
        this.exchDisp = exchDisp;
    }
    public String getTypeDisp() {
        return typeDisp;
    }
    public void setTypeDisp(String typeDisp) {
        this.typeDisp = typeDisp;
    }
}

Test:-

    CustomJSON customJSON = new  Gson().fromJson(result,CustomJSON.class);

But we are getting Exception:-

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 15
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
    at com.google.gson.Gson.fromJson(Gson.java:803)
    at com.google.gson.Gson.fromJson(Gson.java:768)
    at com.google.gson.Gson.fromJson(Gson.java:717)
    at com.google.gson.Gson.fromJson(Gson.java:689)
rajuGT

As per the model, change the json string from

{
"ResultSet": {
    "Query": "Peabody",
    "Result": [
        {
            "symbol": "BTU",
            "name": "Peabody Energy Corporation",
            "exch": "NYQ",
            "type": "S",
            "exchDisp": "NYSE",
            "typeDisp": "Equity"
        },

to

{
"ResultSet": "someResultSetString",        //<-- this should be string
    "Query": "Peabody",
    "Result": [
        {
            "symbol": "BTU",
            "name": "Peabody Energy Corporation",
            "exch": "NYQ",
            "type": "S",
            "exchDisp": "NYSE",
            "typeDisp": "Equity"
        },

ResultSet is String, but you are using { literal considering it as object. Remove it and assign string value to it. Also while removing make sure to remove the corresponding } literal.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

", or } expected while parsing object/hash" error while parsing a JSON file

Error while parsing date from json to object

Error while parsing JSON Object in Java Android

Error while parsing JSON

error while parsing Json in javascript

Error while parsing JSON in python

Json Parsing object inside Object getting error

ERROR when Parsing JSON Array as a JSON Object

Error while parsing JSON babel failure

Error message while parsing json in Python

valueNotFound error while parsing Json response in IOS

Null error while parsing JSON file in Flutter

Getting error while parsing the the JSON data into android

Error while parsing json file with objects

Error while Converting to Json Object

Error while JSON Object request

Json parsing error: Unterminated object at character 275 of

error while parsing JSON file, probally a hidden value on the JSON content

flutter type error while parsing json data using json serialisable

Parsing of JSON returns error while the JSON seems to be fine

Error when deserializing JSON object "Unexpected character encountered while parsing value: <. Path '', line 0, position 0."

'module' object has no attribute 'loads' while parsing JSON using python

AttributeError: 'module' object has no attribute 'loads' while parsing json in python

Error While parsing in JSP

Error parsing a JSON object with Instant type parse error

Parsing json error: java.lang.IllegalStateException: Not a JSON Object:

Android Json parsing string cannot be converted to json object error

Docker build-push action: Error while parsing json secret

Lexical Error while parsing JSON file for chef-client

TOP Ranking

HotTag

Archive