试图获得一个单独的json对象android

时间:2017-03-27 01:45:40

标签: android json

如果有人可以帮助我,我真的会感到沮丧。试图获得" full_name"," Sex"和"位置" if" full_name"约翰"约翰"但没有工作

public class DataParser  extends AsyncTask<Void,Void,Integer>{

Context c;
ListView lv;
String jsonData;

ProgressDialog pd;
ArrayList<Person> persons=new ArrayList<>();

public DataParser(Context c, ListView lv, String jsonData) {
    this.c = c;
    this.lv = lv;
    this.jsonData = jsonData;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();

    pd=new ProgressDialog(c);
    pd.setTitle("Parse");
    pd.setMessage("Parsing...Please wait");
    pd.show();
}

@Override
protected Integer doInBackground(Void... params) {
    return this.parseData();
}

@Override
protected void onPostExecute(Integer result) {
    super.onPostExecute(result);

    pd.dismiss();
    if(result==0)
    {
        Toast.makeText(c,"Unable to parse",Toast.LENGTH_SHORT).show();
    }else {
        //CALL ADAPTER TO BIND DATA
        CustomAdapter adapter=new CustomAdapter(c,persons);
        lv.setAdapter(adapter);
    }
}

 private int parseData()
{
    try {
        JSONObject ja= new JSONObject(jsonData);
        persons.clear();
        Person s=null;

        JSONObject jo=ja.getJSONObject("full_name");

        if (jo.equals("John")) {

            int id = jo.getInt("id");
            String name = jo.getString("full_name");
            String sex = jo.getString("sex");
            String location = jo.getString("location");

            s = new Person();
            s.setId(id);
            s.setFull_name(name);
            s.setSex(sex);
            s.setLocation(location);

            persons.add(s);
        }
                return 1;

    } catch (JSONException e) {
        e.printStackTrace();
    }

    return 0;
}
}

如果有人可以帮助我,我真的会感到沮丧。试图获得&#34; full_name&#34;,&#34; Sex&#34;和&#34;位置&#34; if&#34; full_name&#34;约翰&#34;约翰&#34;但没有工作

1 个答案:

答案 0 :(得分:0)

我猜你有一个人的JSONArray,你想要得到人=约翰的对象。

如果数据是这样的

#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>

using namespace std;

#define NUM_THREADS 4

long int sharedcount;
pthread_mutex_t count_mutex;

//Function that will be run by multiple threads
//Needs to return a void pointer and if it takes arguments
//it needs to be a void pointer
void *ThreadedFunction(void *threadid) 
{
    int success;
    long id = (long)threadid;

    //Lock mutex preventing the other threads from ru nning
    success = pthread_mutex_lock( &count_mutex );
    cout << "Thread " << id << " beginning.\n";
    for(int i = 0; i < 100000000; i++)
        sharedcount++;

    cout << "Thread " << id << " exiting.\n";
    cout << sharedcount << endl;

    //Unlock the mutex after the thread has finished running
    pthread_mutex_unlock( &count_mutex );

    //Kill the thread
    pthread_exit(NULL);
}

int main () 
{
    //Initialize mutex
    pthread_mutex_init(&count_mutex, NULL);

    //Create an array of threads
    pthread_t threads[NUM_THREADS];
    int rc;
    int i;

    sharedcount = 0;

    for( i=0; i < NUM_THREADS; i++ )
    {
        cout << "main() : creating thread, " << i << endl;

        //Create thread by storing it in a location in the array.  Call the
        //function for the threads to run inside.  And pass the argument (if any).
        //If no arguments pass NULL
        rc = pthread_create(&threads[i], NULL, ThreadedFunction, (void *)i);

        if (rc)
        {
             cout << "Error:unable to create thread," << rc << endl;
             exit(-1);
        }
    }

    //Have main thread wait for all other threads to stop running.
    for(i = 0; i < NUM_THREADS; i++)
    pthread_join(threads[i], NULL);

    //cout << sharedcount << endl;

    pthread_exit(NULL);
}

你可以写:

[{
    "full_name": "John Smith",
    "sex": "male",
    "location": "New York, NY, United States"
}, {
    "full_name": "Angela Johnson",
    "sex": "female",
    "location": "San Diego, CA, United States"
}]