0

我有一个包含以下内容的json:

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

我为解析 json 文件编写的代码如下:

    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;
    }
}

我收到以下错误:

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): }

谁能帮我吗。

4

1 回答 1

5

对象中的键/值对用逗号分隔。您的输入缺少它们。

原始和更正(按此顺序):

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

另请参阅JSON Lint的输出:

Parse error on line 4:
...       "word": "abc""meaning": "meaning 
-----------------------^
Expecting '}', ':', ',', ']'
于 2012-08-10T08:17:19.967 回答