-2

i have an issue with getting incorrect JSON result from intent; it comes like the following {StartDateTime=20201115101843, amount=1111, terminalId=1234567812121250}, instead of {"StartDateTime":"20201112102410", "amount":"777", "terminalId":"1234567812121250"}, i was pass the values as the following :

intent.putExtra("StartDateTime", pubBean.getFirstReceiptDatetime());                 
put(ThirdTag.AMOUNT, water.getAmount());
intent.putExtra("Terminal ID", pubBean.getPosID());

please advice?

Tugay
  • 2,057
  • 5
  • 17
  • 32
  • welcome to stack overflow please reframe your questions and highlight code by selecting then pressing ctrl+k –  Nov 16 '20 at 07:51
  • Why do you believe that `intent.putExtra()` has anything to do with JSON? It doesn't. If you want JSON, you need to use the JSON API. [`Intent`](https://developer.android.com/reference/android/content/Intent) doesn't give that to you for free. – Andreas Nov 16 '20 at 08:14
  • Good point. That looks like what you get from `HashMap.toString()` ... for example ... which is not *intended* to be JSON. – Stephen C Nov 16 '20 at 08:16
  • Stephen C- String getFirstReceiptDatetime(), Long getAmount, String getPosID() – Amjad Khalid Nov 16 '20 at 08:17

1 Answers1

0

I don't know what your data model looks like but this is a simple example :

  • Pass the JSONObject :

    JSONObject data = new JSONObject("{\"StartDateTime\":\"20201112102410\", \"amount\":\"777\", \"terminalId\":\"1234567812121250\"}");
    
    Intent intent = new Intent(this, SecondActivity.class);
    intent.putExtra("data", data.toString());
    startActivity(intent);
    

Then get the value in other Activity :

JSONObject dataFromFirtstActivity = new JSONObject(getIntent().getStringExtra("data"));
  • Pass the specific value :

    Intent intent = new Intent(this, SecondActivity.class);
    intent.putExtra("startDateTime", data.getLong("startDateTime"));
    intent.putExtra("amount", data.getInt("amount"));
    intent.putExtra("terminalId", data.getString("terminalId"));
    startActivity(intent);
    

Then get the value in other Activity :

  long startDateTime = getIntent().getLongExtra("startDateTime", 0);
  int amount = getIntent().getIntExtra("amount", 0);
  String terminalId = getIntent().getStringExtra("terminalId");

You should deal with your data first before going any further, and you can read more about passing data in here

MrX
  • 953
  • 2
  • 16
  • 42