简体   繁体   中英

how do i convert the JSON file that i got to digits so that i can manipulate them on android

hi im making a currency converter

i've made this part... i am getting the JSON file and i can see it on the LOGCAT... now my problem is that how can i get the JSON to convert it to string and parse it to float/double to enable me to manipulate them...

here is the code so far

import com.actionbarsherlock.app.SherlockActivity;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;


import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends SherlockActivity {

    private static final String API_URL = "my own api";

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

        Button btnConvert = (Button)findViewById(R.id.buttonConvert);

        btnConvert.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                AsyncHttpClient client = new AsyncHttpClient();
                client.get(API_URL, new AsyncHttpResponseHandler() {

                    @Override
                    public void onFailure(Throwable arg0, String arg1) {
                        // TODO Auto-generated method stub
                        super.onFailure(arg0, arg1);
                    }

                    @Override
                    public void onFinish() {
                        // TODO Auto-generated method stub
                        super.onFinish();
                    }

                    @Override
                    public void onStart() {
                        // TODO Auto-generated method stub
                        super.onStart();
                    }

                    @Override
                    public void onSuccess(String response) {

                        Log.i("CC", response);
                    }
                });

            }
        });



    }



}

i am really at a lost right now with a deadline coming at my doorstep

json parsing depend on json string if your string start with an [ that means it is array and if it start with this { that mean it is object. if it is array then get it like this

JSONArray ja=new JSONArray(string);

and if it is object then get it like

JSONObject jo=new JSONObject(string)

now the next thing is getting inside value of this array or object. if you are in array then get object by there index value

JSONObject jo=ja.getJSONObject(index);

now there will be three case first it may be array or second it will be object and third value will be string that you want. if it is array get it like

JSONArray ja1=ja.getJSONArray(arraynamefromyourjsonstring);

now if it is object then you get it like

JSONObject jo1=ja.getJSONObject(objectnamefromyourjsonstring);

and the last is string you want is getting by this

String str=jo1.getString(stringnamefromyourjsonstring)

I've used the Google Gson library with great success. It is dead simple and perhaps importantly uses the Apache V2 license. Google Gson

First of all, get the JSONObject and then extract the string from there like the following and parse it into the desired primitive type:

JSONObject c = json.getJSONObject(i);
String vtype = c.getString(TAG_VTYPE);
float f=Float.parseFloat(vtype);

This is just an example. Obviously you'll replace i, tag_type according to your needs.

Use the org.json library:

Integer i = null;
try {
     JSONObject o = new JSONObject(response);
     i = o.getInt("number_key");
} catch (JSONException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
}

if(i != null) {
     // We're good
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM