-2

Hi i have a JSON file saved in a txt file and i am parsing it in my android code. I keep getting a JSONSyntax lateral exception. I used a json validator and my json syntax is correct. I really don't know the problem. Can someone pls help me out. I used this JSON object array in my file

    {"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"
        }
    ]
}

This is my android class fragment where i access my json objects:

        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;

}
}

This is my logcat error ouput:

 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)

'

4

2 回答 2

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");
 }
于 2014-06-02T09:14:27.217 回答
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();
}
于 2014-06-02T09:22:36.727 回答