How do I send and parse Json objects on Android?

Asked 1 years ago, Updated 1 years ago, 100 views

I'm trying to send a message from the server in Json format, receive it in Json format, and process it

I think I'll get it from the server in the following format { "post": { "username": "someusername", "message": "this is a sweet message", "image": "http://localhost/someimage.jpg", "time": "present time" } }

How much knowledge do I need about JSON to do this? Please teach me how to send and receive JSON.

android parsing json

2022-09-22 15:43

1 Answers

Json's foundation is http://luckyyowu.tistory.com/303 http://lonelycat.tistory.com/374

I think it's well explained here.

Parsing and receiving JSON from Android

 String str =
    "[{'username':'someusername','message':'this is a sweet message,'image':'http://localhost/someimage.jpg','time':'present time'},"+
    "{'username':'someusername','message':'this is a sweet message,'image':'http://localhost/someimage.jpg','time':'present time'}]";

Store data to send to String objects in this way.

JSONArray jarray = new JSONArray(str); Save to JSONARRAY object.

Extract child objects in order with the getJSONObject(i) method of the JSONarray object The length() method can be used to check the number of children. The extracted child objects are received as JSONObject.

for(int i=0; i < jarray.length(); i++){
    JSONObject jObject = array.getJSONObject(i); // extract JSONObject
     .....
}

Each JSONObject object receives a 'value' via the get*(~) method. The parameter for the get* method is the 'key' value.

String address = jObject.getString("address");
String name = jObject.getString("name");
int age = jObject.getInt("age");

MainActivity is as follows.

public class MainActivity extends ActionBarActivity {

    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv = (TextView)findViewById(R.id.tvResult1);
        Button b = (Button) findViewById(R.id.button);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                doJSONParser();
            }
        });
    } } // end onCreate()

    void doJSONParser(){
        StringBuffer sb = new StringBuffer();

        String str =
                "[{'name':'Batman','age':43,'address':'Gotham'}",+
                "{'name':'Superman','age':36',address':'New York'}",+
                "{'name':'Antman','age':25',address':'LA'}]";

        try {
            JSONARRAY = new JSONARRAY(str); // Create JSONARRAY
            for(int i=0; i < jarray.length(); i++){
                JSONObject jObject = array.getJSONObject(i); // extract JSONObject
                String address = jObject.getString("address");
                String name = jObject.getString("name");
                int age = jObject.getInt("age");

                sb.append(
                        "Address:" + address +
                        "Name:" + name +
                        "Age:" + age + "\n"
                    );
            }
            tv.setText(sb.toString());
        } } catch (JSONException e) {
            e.printStackTrace();
        }
    } } // end doJSONParser()
}  }  // end class

You can also apply it properly and send it.


2022-09-22 15:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.