java.lang..String类型的值无法转换为JSONArray

时间:2016-02-13 20:25:05

标签: android json

这些是我在android中制作Json的代码,当我运行程序时我有一个例外!请帮助我

公共类MyActivity扩展了Activity {

   Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    button= (Button) findViewById(R.id.button);
     try {


         AssetManager manager = getResources().getAssets();
         InputStream Is= manager.open("myjson.json");
         String result="";
         int c = 0;
         while ((Is.read())!=-1){

             result +=(char)c;

         }
         Is.close();

         JSONArray jsonArray = new JSONArray(result);
         final JSONObject object=jsonArray.getJSONObject(0);


         button.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                  try {


                      Toast.makeText(getApplicationContext(), "id is:" + object.getString("Day"), Toast.LENGTH_LONG).show();
                      Toast.makeText(getApplicationContext(), "name is:" + object.getString("Start"), Toast.LENGTH_LONG).show();
                      Toast.makeText(getApplicationContext(), "type is:" + object.getString("Finish"), Toast.LENGTH_LONG).show();


                  }catch (Exception e){
                      Toast.makeText(getApplicationContext(),e+"",Toast.LENGTH_LONG).show();
                  }
                  }

         });


     }catch (Exception e){
         Toast.makeText(getApplicationContext(),e+"",Toast.LENGTH_LONG).show();
     }

}

}

这些是Json代码:

[
    {
        "Id": 5207,
        "Name": "Эриван",
        "Types": [
            "Ресторан"
        ],
        "Latitude": 53.904752,
        "Longitude": 27.521095,
        "OperatingTime": [
            {
                "Day": 1,
                "Start": "10:00:00",
                "Finish": "23:00:00"
            },
            {
                "Day": 2,
                "Start": "10:00:00",
                "Finish": "23:00:00"
            },
            {
                "Day": 3,
                "Start": "10:00:00",
                "Finish": "23:00:00"
            },
            {
                "Day": 4,
                "Start": "10:00:00",
                "Finish": "23:00:00"
            },
            {
                "Day": 5,
                "Start": "10:00:00",
                "Finish": "23:00:00"
            },
            {
                "Day": 6,
                "Start": "08:00:00",
                "Finish": "23:00:00"
            },
            {
                "Day": 0,
                "Start": "08:00:00",
                "Finish": "23:00:00"
            }
        ],
        "IsBookingAvailable": false
    }]

1 个答案:

答案 0 :(得分:1)

更改

Toast.makeText(getApplicationContext(), "id is:" + object.getString("Day"), Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "name is:" + object.getString("Start"), Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "type is:" + object.getString("Finish"), Toast.LENGTH_LONG).show();

// object.getString("Id")
Toast.makeText(getApplicationContext(), "id is:" + object.getString("Id"),
         Toast.LENGTH_LONG).show();
// object.getString("Name")
Toast.makeText(getApplicationContext(), "name is:" + object.getString("Name"), 
         Toast.LENGTH_LONG).show();
// object.getJSONArray("Types").toString()
Toast.makeText(getApplicationContext(), "type is:" + 
        object.getJSONArray("Types").toString(), Toast.LENGTH_LONG).show();

“Day”“开始”&无法直接通过object访问“完成”。它们位于“OperatingTime” JSONObject的几个嵌套JSONArray内。