0

these are my codes for make a Json in android and i have an exception when i run the programm!! plz help me

public class MyActivity extends 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();
     }

}

}

and these are the Json codes:

[
    {
        "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
    }]
2
  • It would probably help if you were to add the exception stack trace to the question. Also, since we can't really tell line numbers from code snippets, point out the relevant line. Commented Feb 13, 2016 at 20:36
  • i changed the Toasts but it did not answer!!! Commented Feb 13, 2016 at 20:49

1 Answer 1

1

Change

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();

to

// 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", "Start" & "Finish" are not accessible directly via object. They are located within several nested JSONObjects of the "OperatingTime" JSONArray.

Sign up to request clarification or add additional context in comments.

5 Comments

This is not an answer, this is Java naming convention.
@Stanojkovic I'm not sure that I understand what you've just said.
@MohammedAoufZOUAG I don't think he realized that you changed the objects being retrieved from the JSON
@davehenry seems like it
i can't resolve my problem!!! the exception is for the first try,catch!! these codes have a problem!! 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);

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.