0

i'm working on Android with Eclipse. I'm writing a project where my app receives (also sends) data from a mysql database. I wrote a php file to make the project to work. I'm using JSON to communicate with the php file hosted in my local server. I don't have problems with the connection or similar problems but i receive this type of the json data:

   {"field1":"A","field2":"abcbab","field3":5},
    {"field1":"B","field2":"fakfef","field3":25}, 
    {"field1":"A","field2":"faefe","field3":12},
    {"field1":"C","field2":"aafefe","field3":30},
    {"field1":"C","field2":"caere","field3":30},

etc..

Now my Android app has the target to create three different arrays (one for the type A of field1, one for the B and one for the C) to subsequent processing from the JSON data. I have two problems here.

I don't understand how to select a json value from a particular value of a field. I try to explain better, i know these instructions:

 JSONObject json_data = new JSONObject(result);

 json_data.getString("field1");

But how to say: "if field1 is A then put the value of field2 in arrayA1 and field3 in arrayA2", "if field1 is B then put the value of field2 in arrayB1 and field3 in arrayB2", "if field1 is C then put the value of field2 in arrayC1 and field3 in arrayC2". maybe I'm making it harder than it looks but i don't know i to do that. In this way i have two arrays for each type of field1, there is a better way to get what i want?. Remember: i can't change the code in the php file so the output that i get is always the rows shown above.

Remees M Syde
  • 2,564
  • 1
  • 19
  • 42
tittarella
  • 15
  • 6
  • You must read every line as a different JSONObjects and then process every line independently. You can split your result (a String I guess) by the coma character. Then in a for loop process every token of the split as a JSONObject. Did you understand me? I'm so sleepy right now. – CaptainTeemo Oct 20 '14 at 09:52
  • Can you give me a short example? Thanks for your support. – tittarella Oct 20 '14 at 10:47
  • http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java – CaptainTeemo Oct 21 '14 at 05:16
  • Sorry but i want to use methods with JSONObject and JSONArray on Java. It's not possible? – tittarella Oct 23 '14 at 07:40

0 Answers0