• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps JSON Parser help

HeavensSentSword

Well-Known Member
Jan 19, 2016
134
36
Ok so I have been working on getting this JSON parser to work and I feel the code is right, but I cannot get any results from it. It keeps giving me that the output is Null Node. i am kind of stumped on this as I dont know what I did wrong.

So here is the main activity where the button is pushed it should call the JSONparser and send the internal json file to it.

Java:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;



public class MainActivity extends AppCompatActivity {

    String TAG = "Test ME";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button bparsejson      = (Button) findViewById(R.id.bparsejson);
        final TextView output = (TextView) findViewById(R.id.output);
        final String strJson = "{ \"Android\" :[{\"song_name\":\"Gimme Dat\",\"song_id\":\"1932\",\"artist_name\":\"Sidney Samson (Feat. Pitbull & Akon)\"},{\"song_name\":\"F-k The Money (Remix)\",\"song_id\":\"73\",\"artist_name\":\"B.o.B. (Feat. Wiz Khalifa)\"}] }";
        final String outputData = null;
        Log.d(TAG, "I have started");
        /******** Listener for button click ********/
        bparsejson.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "Button Pushed");
                JSONParser jp = new JSONParser();
                jp.parsesData(strJson, outputData);
                output.setText(outputData);
            }
        });


    }


}


And here is the JSON parser code .
Java:
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;



/**
* Created by MMILLAR on 1/20/2016.
* JSON Parser Class
* USe this to parse all data which is JSON
*/
public class JSONParser {
    JSONObject jsonResponse;
    //String OutputData = "";
    //String jsonData = null;
    String TAG = "JSON Output: ";

    //Consturctor
    public JSONParser()
    {
        //this.jsonData = jsonData;
       // this.OutputData = outPutData;
    }

    public String parsesData(String jsonData, String outputData)
    {
        try
        {
            //Creaate a new JSONObject ith the name/value mapping from the JSON string
            jsonResponse = new JSONObject(jsonData);
            //Returns the value mapped by the name if it exists and is a JSONArry
            JSONArray jsonMainNode = jsonResponse.optJSONArray("Android");

            //Proccess the JSON node
            int lenghtJsonArrar = jsonMainNode.length();
            for (int i = 0; i<lenghtJsonArrar; i++)
            {
                //Get object for each json node
                JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                //Get the node values
                int song_id = Integer.parseInt(jsonChildNode.optString("song_id").toString());
                String song_name = jsonChildNode.optString("song_name").toString();
                String artist_name = jsonChildNode.optString("srtist_name").toString();


                //create the output
                outputData += "Node: \n\n " + song_id + " | "
                                            + song_name + " | "
                                            + artist_name + " \n\n ";

                Log.d(TAG, outputData);
            }

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

}


Thank you for any help with this I am at a loss of why it is not working.
 
Last edited:
Well I can see a typo immediately

Code:
                String artist_name = jsonChildNode.optString("srtist_name").toString();

If your program is not doing what you expect, the best thing you can do to diagnose the problem, is to breakpoint and step through the code.
So run up your app in debug mode, and set a breakpoint at line 33 in the JSONParser class. Then step through, line by line, and look at the values of the variables. Continue stepping through the loop, paying particular attention to the values song_id, song_name and artist_name.
 
  • Like
Reactions: HeavensSentSword
Upvote 0
No that in itself should not cause the whole to to stop working, but I would expect artist_name to be null. It's not an error as such, just a failure to give you the results you expect.
I haven't used the Json parser before, so cannot offer any more insight into the problem. But I would be debugging this, and checking that I was using the Json parser API in the right way.
 
  • Like
Reactions: HeavensSentSword
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones