8

I am trying to read JSON file from asset folder. But I get the following exception:

org.json.JSONException: Expected literal value at character 550

I searched lot of stuff but didn't find anything relevant. Here is my JSON file:

{
"resultCount": 3,
"SearchedTerm": "Wada Pav",
"results": [
{
  "locationname": "Mahableshwar Hotel",
  "locationid": "12345",
  "locationaddress" : "baner, Pune",
  "dishrating": "4",
  "dishname": "Wada Pav",
  "dishid": "234",
  "dishcategory": "Snacks",
  "dishnotes": "Spicy Wada Pav",
  "dishpreviewurl":"http://xxx.yyy.zzz/mahableshwar/1.jpg",
  "dishtotalvotes": "9999",
  "friendslistvoted": {
            "friendscount": "3",
            "names": ["Santosh","Sandip","Arvind"],
          }
  "dishimageurl": "http://xxx.yyy.zzz/mahableshwar/2.jpg",
  "mylastrating": "4"
 } 
]
}

I find JSON object on 550 is "names": ["Santosh","Sandip","Arvind"],. I am trying to solve it but don't know what happens in my code. Here is my code:

try {

        input = assetManager.open("dishum-sample-search-results-json.txt");
        int size = input.available();
        byte[] buffer = new byte[size];
        input.read(buffer);
        input.close();
        
        // byte buffer into a string
        String text = new String(buffer);
        try{
        JSONObject jsonObject = new JSONObject(text);
         
        JSONObject searchedTerm = jsonObject.getJSONObject(TAG_SEARCHEDTERM);           
        JSONArray results = searchedTerm.getJSONArray(TAG_RESULTS);         
        for(int i=0; i<results.length(); i++)
        {
            JSONObject dishResult = results.getJSONObject(i);
            String locationName = dishResult.getString(TAG_lOCATIONNAME);
            int locationId = dishResult.getInt(TAG_LOCATIONID);
            String locationAddress = dishResult.getString(TAG_LOCATIONADDRESS);
            String dishNotes = dishResult.getString(TAG_DISHNOTES);
            int dishRating = dishResult.getInt(TAG_DISHRATING);
            JSONObject friendListVoted = dishResult.getJSONObject(TAG_FRIENDLISTVOTED);
            int friendCount = friendListVoted.getInt(TAG_FRIENDCOUNT);
            map.put(TAG_lOCATIONNAME, locationName);
            map.put(TAG_LOCATIONID, String.valueOf(locationId));
            map.put(TAG_LOCATIONADDRESS, locationAddress);
            map.put(TAG_DISHNOTES, dishNotes);
            map.put(TAG_DISHRATING, String.valueOf(dishRating));
            map.put(TAG_FRIENDCOUNT, String.valueOf(friendCount));              
            // adding HashList to ArrayList
            songsList.add(map); 
        }
        }
        catch(JSONException e)
        {
            Toast.makeText(this, "Error in parsing json file"+e, Toast.LENGTH_LONG).show();
        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

I also debug my code but when control goes on JSONObject jsonObject = new JSONObject(text); it throw exception and goes in first catch block. How can I solve this problem?

halfer
  • 19,824
  • 17
  • 99
  • 186
Sandip Armal Patil
  • 6,241
  • 21
  • 93
  • 160
  • (Editor's note: two items of code were removed from this question, and I can't work out why. Since the accepted answer relies at least on one of them, I have restored them both). – halfer Aug 04 '23 at 23:35

4 Answers4

18

your JSON is invalid.
your JSON should look like this

{
    "resultCount": 3,
    "SearchedTerm": "Wada Pav",
    "results": [
        {
            "locationname": "Mahableshwar Hotel",
            "locationid": "12345",
            "locationaddress": "baner, Pune",
            "dishrating": "4",
            "dishname": "Wada Pav",
            "dishid": "234",
            "dishcategory": "Snacks",
            "dishnotes": "Spicy Wada Pav",
            "dishpreviewurl": "http://xxx.yyy.zzz/mahableshwar/1.jpg",
            "dishtotalvotes": "9999",
            "friendslistvoted": {
                "friendscount": "3",
                "names": [
                    "Santosh",
                    "Sandip",
                    "Arvind"
                ]
            },
            "dishimageurl": "http://xxx.yyy.zzz/mahableshwar/2.jpg",
            "mylastrating": "4"
        }
    ]
}

try using a JSON validator before using it (like JSLint).

thepoosh
  • 12,497
  • 15
  • 73
  • 132
9

I'm using following to get standard JSON format. This one is better.

    public static String convertStandardJSONString(String data_json) {
        data_json = data_json.replaceAll("\\\\r\\\\n", "");
        data_json = data_json.replace("\"{", "{");
        data_json = data_json.replace("}\",", "},");
        data_json = data_json.replace("}\"", "}");
        return data_json;
    }
Zin Win Htet
  • 2,448
  • 4
  • 32
  • 54
  • Excellent. It worked, but I don't know why I needed this in the first place, since I'm using drupal_json_encode!!! Is drupal not standard? – Milad.Nozari Jul 21 '15 at 05:20
4

I use jsoneditoronline online tool that works pretty good.

enter image description here

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
1

this works for me

    public static String convertJSONString(String data) {
    data = data.replace("\\", "");
    return data; }