1

I have a string output from server and I am trying to extract some values form the string.

Here is the output from server:

jsonString =

{
  "MEANING":"reduce",
  "DISPLAY":"",
  "TYPE_CD":1,
  "SELECTED_IND":1,
  "CNT":1,
  "SOURCES":[
              { "a":1 }
            ]
}

Code:

JsonReader reader = new JsonReader(new StringReader(jsonString));
DataObject obj1 = new Gson().fromJson(reader, DataObject.class);

DataObject Class:

DataObject
{ 
    private int MEANING;
    private int CNT;
    private String TYPE_CD;
    private String DISPLAY;
    private String MEANING;
    private List<Long> SOURCES;

    public String getSourceTypeMeaning()
     {
       return this.MEANING;
     }

    public String getSourceTypeDisplay() 
     {
       return this.DISPLAY;
     }

    public String getSourceTypeCd() 
     {
       return this.TYPE_CD;
     }

    public int getSourceCount() 
     {
       return this.CNT;
     }

    public List<Long> getSourceList() 
     {
       return this.SOURCES;
     }
}

but getting this error

Expected a string but was BEGIN_OBJECT at line 1 column 132

I am not able to find the issue with my code.

3 Answers 3

1

Other answers are pointing out that the problem is in the SOURCES field, and that's true, but the solutions they're giving are not correct...

You can't use just a Map to parse the SOURCES field, because this field is indeed an array! You have:

"SOURCES": [ ... ]

Since you have square brackets [ ], you have an array! And it's true there's a Map, but it is contained in the array...

So, what you need to parse that field correctly is:

private List<Map<String, int>> SOURCES;

Note that we use a Map to allow the content of SOURCES to have multiple and unknown values, so that this code could parse not only your JSON, but also something like:

"SOURCES":[
              { "a":1, "b":2 },
              { "c":3 },
              { "x":99, "y":98, "z":97 }
          ]
Sign up to request clarification or add additional context in comments.

Comments

0

SOURCES variable should be Map<String,Long>,because in JSON string SOURCES is key-value collection ("a":1) where "a" is string and 1 is number.

Hope this helps.

2 Comments

I changed my code to use HashMap, still getting a error. private HashMap<String, Long> SOURCES; public HashMap<String, Long> getSourceList() { return this.SOURCES; } Still getting an error saying "Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1"
-1, wrong answer: SOURCES field is clearly a JSON array since it has square brackets [ ]...
0

Check this

"SOURCES":[
              { "a":1 }
 ]

This will represent a List of map not List of long.

So change your List<long> to List<Map<String, Long>> or List<Map<Object, Long>>.

2 Comments

@shanku sorry to say but I didn't copy the ans. When I submit my ans after that I see that the same ans is already submitted and when I start writting ans there is no ans present at that time.
-1, wrong answer: SOURCES field does represent a List since it has square brackets [ ]...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.