解析JSON会给出错误org.json.JSONException:预期的文字值

时间:2014-06-02 09:06:29

标签: java android json android-json

嗨我有一个保存在txt文件中的JSON文件,我在我的android代码中解析它。我一直在获得JSONSyntax横向异常。我使用了json验证器,我的json语法是正确的。我真的不知道这个问题。有人可以帮助我。 我在我的文件中使用了这个JSON对象数组

    {"destinations": [
        {
            "name": "Aegean Coast",
            "id": "001"
        },
        {
            "name": "Black Sea",
            "id": "002"
        },
        {
            "name": "Central and eastern Anatolia",
            "id": "003"
        },
        {
            "name": "Istanbul",
            "id": "004"
        },
        {
            "name": "Lycian Way",
            "id": "005"
        },
        {
            "name": "Georgia",
            "id": "006"
        }
    ]
}

这是我的android类片段,我可以访问我的json对象:

        public class DestinationsFragment extends Fragment {
    private GoogleMap map;



     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {


            View rootView = inflater.inflate(R.layout.fragment_destinations, container, false);
            ListView listview= (ListView)rootView.findViewById(R.id.list);
            String feed = loadJSONFromAsset();

            try {
                 ArrayList<String> items = new ArrayList<String>();
              JSONArray jsonArray = new JSONArray(feed);
              for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
             String name= jsonObject.getString("name");
             items.add(name);
             Log.d(name,"Output");
              }
              ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),R.layout.list_item, items);
              listview.setAdapter(adapter);

            } catch (Exception e) {
              e.printStackTrace();
            }


            return rootView;
        }



     public String loadJSONFromAsset() {
            String json = null;
            try {

                InputStream is = getActivity().getAssets().open("destinations.txt");
                int size = is.available();
                byte[] buffer = new byte[size];
                is.read(buffer);
                is.close();
                json = new String(buffer, "UTF-8");

            } catch (IOException ex) {
                ex.printStackTrace();
                return null;
            }
            return json;

}
}

这是我的logcat错误输出:

 06-02 11:51:47.027: W/System.err(6304): org.json.JSONException: Expected literal value at character 1 of {\rtf1\ansi\ansicpg1252\cocoartf1265\cocoasubrtf200
06-02 11:51:47.027: W/System.err(6304): {\fonttbl\f0\fmodern\fcharset0 CourierNewPSMT;}
06-02 11:51:47.027: W/System.err(6304): {\colortbl;\red255\green255\blue255;}
06-02 11:51:47.027: W/System.err(6304): \paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0
06-02 11:51:47.027: W/System.err(6304): \deftab720
06-02 11:51:47.027: W/System.err(6304): \pard\pardeftab720
06-02 11:51:47.027: W/System.err(6304): \f0\fs28 \cf0 \{"destinations": [\
06-02 11:51:47.027: W/System.err(6304):         \{\
06-02 11:51:47.027: W/System.err(6304):             "name": "Aegean Coast",\
06-02 11:51:47.027: W/System.err(6304):             "id": "001"\
06-02 11:51:47.027: W/System.err(6304):         \},\
06-02 11:51:47.027: W/System.err(6304):         \{\
06-02 11:51:47.027: W/System.err(6304):             "name": "Black Sea",\
06-02 11:51:47.027: W/System.err(6304):             "id": "002"\
06-02 11:51:47.027: W/System.err(6304):         \},\
06-02 11:51:47.027: W/System.err(6304):         \{\
06-02 11:51:47.027: W/System.err(6304):             "name": "Central and eastern Anatolia",\
06-02 11:51:47.027: W/System.err(6304):             "id": "003"\
06-02 11:51:47.027: W/System.err(6304):         \},\
06-02 11:51:47.027: W/System.err(6304):         \{\
06-02 11:51:47.027: W/System.err(6304):             "name": "Istanbul",\
06-02 11:51:47.027: W/System.err(6304):             "id": "004"\
06-02 11:51:47.027: W/System.err(6304):         \},\
06-02 11:51:47.027: W/System.err(6304):         \{\
06-02 11:51:47.027: W/System.err(6304):             "name": "Lycian Way",\
06-02 11:51:47.027: W/System.err(6304):             "id": "005"\
06-02 11:51:47.027: W/System.err(6304):         \},\
06-02 11:51:47.027: W/System.err(6304):         \{\
06-02 11:51:47.027: W/System.err(6304):             "name": "Georgia",\
06-02 11:51:47.027: W/System.err(6304):             "id": "006"\
06-02 11:51:47.027: W/System.err(6304):         \}\
06-02 11:51:47.027: W/System.err(6304):     ]\
06-02 11:51:47.027: W/System.err(6304): \}}
06-02 11:51:47.037: W/System.err(6304):     at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
06-02 11:51:47.037: W/System.err(6304):     at org.json.JSONTokener.readLiteral(JSONTokener.java:285)
06-02 11:51:47.037: W/System.err(6304):     at org.json.JSONTokener.nextValue(JSONTokener.java:111)
06-02 11:51:47.037: W/System.err(6304):     at org.json.JSONTokener.readObject(JSONTokener.java:362)
06-02 11:51:47.037: W/System.err(6304):     at org.json.JSONTokener.nextValue(JSONTokener.java:100)
06-02 11:51:47.037: W/System.err(6304):     at org.json.JSONArray.<init>(JSONArray.java:87)
06-02 11:51:47.037: W/System.err(6304):     at org.json.JSONArray.<init>(JSONArray.java:103)

&#39;

2 个答案:

答案 0 :(得分:4)

你的json看起来很有效。

 { // Json object node 
 "destinations": [ // json array destinations 
  {          // json object node 
    "name": "Aegean Coast", 
    "id": "001"
  },

您的json不是JSONArray,而是JSONObject。你的解析似乎不对

更改此

 JSONArray jsonArray = new JSONArray(feed);

JSONObject jsonObject = new JSONObject(feed); 

然后

JSONArray jsonArray = jsonObject.getJSONArray("destinations");
 for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonObject = jsonArray.getJSONObject(i);
        String name= jsonObject.getString("name");
        items.add(name);
        Log.d(name,"Output");
 }

答案 1 :(得分:0)

请使用以下代码代替您的json解析代码,它将解决您的问题。

try {
    ArrayList<String> items = new ArrayList<String>();
    JSONObject mJsonObj = new JSONObject(feed);
    JSONArray jsonArray = mJsonObj.getJSONArray("destinations");
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonObject = jsonArray.getJSONObject(i);
        String name= jsonObject.getString("name");
        items.add(name);
        Log.d(name,"Output");
    }
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),R.layout.list_item, items);
    listview.setAdapter(adapter);
} catch (Exception e) {
    e.printStackTrace();
}