0

I have a json with the following contents:

{
    "Dictionary":[
        {
            "word": "abc"
            "meaning": "meaning of abc"
        },
        {
            "word": "def"
            "meaning": "meaning of def"
        }
        ]
}

The code which I have written for parsing the json file is as follows:

    public class JSONParser extends Activity
    {
         static JSONObject jObj = null;
        static String json = "";

        // constructor
        public JSONParser() 
        {

        }
public JSONObject getJSONFromInput(InputStream location) 
    {
InputStream isr = location;
Writer writer = new StringWriter();
            char[] buffer = new char[1024];
            try
            {
                Reader reader = new BufferedReader(new InputStreamReader(isr,"UTF-8"));
                int n;
                while ((n = reader.read(buffer)) != -1) 
                {
                    writer.write(buffer, 0, n);
                }
                isr.close();
                json = writer.toString();
            }
            catch (Exception e) 
            {
                Log.e("Buffer Error", "Error converting result " + e.toString());
            }
try 
            {
                jObj = new JSONObject(json);
            } 
            catch (JSONException e) 
            {
                Log.e("JSON Parser", "Error parsing data " + e.toString());
            }

        // return JSON String
        return jObj;
    }
}

I'm getting the below Error:

E/JSON Parser(555): Error parsing data org.json.JSONException: Unterminated object at character 47 of {
E/JSON Parser(555): "Dictionary":[
E/JSON Parser(555): {
E/JSON Parser(555):     "word": "abc"
E/JSON Parser(555):     "meaning": "meaning of abc"
E/JSON Parser(555): },
E/JSON Parser(555): {
E/JSON Parser(555):     "word": "def"
E/JSON Parser(555):     "meaning": "meaning of def"
E/JSON Parser(555): }
E/JSON Parser(555): ]
E/JSON Parser(555): }

Can anyone help me out.

1 Answer 1

5

Key/Value pairs in objects are separated by commas. Your input is missing them.

Original and corrected (in that order):

    { "word": "abc" "meaning": "meaning of abc" },
    { "word": "abc", "meaning": "meaning of abc" },

See also, the output of JSON Lint:

Parse error on line 4:
...       "word": "abc""meaning": "meaning 
-----------------------^
Expecting '}', ':', ',', ']'
Sign up to request clarification or add additional context in comments.

1 Comment

This is the first time I'm using JSON. Therefore made that silly error.

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.