0

hello everyone i try to parse json .i have simple json like thislike this

i try to parse this json but result is null .i have no idea what i'm doing wrong this is a my code

public class HomeActivity extends Activity {
private String URL = "*****************";
private TextView PNumber;

JSONArray jsonArray = null;

public static String KEY_PNumber = "PNumber";
String id;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);
    PNumber = (TextView) findViewById(R.id.PNumber);
    new LoadDataAllChanelsToServer().execute();
}

private class LoadDataAllChanelsToServer extends
        AsyncTask<String, Integer, String> {

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected String doInBackground(String... urls) {

        JSONParser jParser = new JSONParser();


        JSONObject json = jParser.getJSONFromUrl(URL);

         try {

    JSONObject jsonObject = json.getJSONObject("data");

    id = jsonObject.getString("PNumber");
} catch (JSONException e) {
    e.printStackTrace();
}

        return null;

    }

    @Override
    protected void onPostExecute(String result) {
        PNumber.setText(id);
    }

}

}

what am i doing wrong if anyone knows solution help me thanks

i have this errror error occurred while executing doinbackground()

1 Answer 1

1

The Json you posted does not contain any array. Its a JSON object. A Json array starts with [. You can read more about JSON from here.

Now, regarding parsing:

Do it as follows:

    try {

        JSONObject jsonObject = json.getJSONObject("data");

        String pNumber = jsonObject.getString("PNumber");
    } catch (JSONException e) {
        e.printStackTrace();
    }
Sign up to request clarification or add additional context in comments.

5 Comments

i changed it but nothing happened
Have you checked the logcat?Just make sure no JSONException has occured during parsing.
And try to print the response you got from server
at the moment i have this error ----android asynctask error occurred while executing doinbackground() what i am doing wrong ?
Post your full logcat stack

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.