1. Are you ready for the Galaxy S20? Here is everything we know so far!

JSON Parser help

Discussion in 'Android Development' started by HeavensSentSword, Jan 20, 2016.

  1. HeavensSentSword

    HeavensSentSword Well-Known Member
    Thread Starter

    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.

    Code (Java):
    1.  
    2. import android.os.Bundle;
    3. import android.support.v7.app.AppCompatActivity;
    4. import android.util.Log;
    5. import android.view.View;
    6. import android.widget.Button;
    7. import android.widget.TextView;
    8. import android.view.View.OnClickListener;
    9.  
    10.  
    11.  
    12. public class MainActivity extends AppCompatActivity {
    13.  
    14.     String TAG = "Test ME";
    15.  
    16.     @Override
    17.     protected void onCreate(Bundle savedInstanceState) {
    18.         super.onCreate(savedInstanceState);
    19.         setContentView(R.layout.activity_main);
    20.         final Button bparsejson      = (Button) findViewById(R.id.bparsejson);
    21.         final TextView output = (TextView) findViewById(R.id.output);
    22.         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)\"}] }";
    23.         final String outputData = null;
    24.         Log.d(TAG, "I have started");
    25.         /******** Listener for button click ********/
    26.         bparsejson.setOnClickListener(new OnClickListener() {
    27.             @Override
    28.             public void onClick(View v) {
    29.                 Log.d(TAG, "Button Pushed");
    30.                 JSONParser jp = new JSONParser();
    31.                 jp.parsesData(strJson, outputData);
    32.                 output.setText(outputData);
    33.             }
    34.         });
    35.  
    36.  
    37.     }
    38.  
    39.  
    40. }
    41.  

    And here is the JSON parser code .
    Code (Java):
    1.  
    2. import android.util.Log;
    3.  
    4. import org.json.JSONArray;
    5. import org.json.JSONException;
    6. import org.json.JSONObject;
    7.  
    8.  
    9.  
    10. /**
    11. * Created by MMILLAR on 1/20/2016.
    12. * JSON Parser Class
    13. * USe this to parse all data which is JSON
    14. */
    15. public class JSONParser {
    16.     JSONObject jsonResponse;
    17.     //String OutputData = "";
    18.     //String jsonData = null;
    19.     String TAG = "JSON Output: ";
    20.  
    21.     //Consturctor
    22.     public JSONParser()
    23.     {
    24.         //this.jsonData = jsonData;
    25.        // this.OutputData = outPutData;
    26.     }
    27.  
    28.     public String parsesData(String jsonData, String outputData)
    29.     {
    30.         try
    31.         {
    32.             //Creaate a new JSONObject ith the name/value mapping from the JSON string
    33.             jsonResponse = new JSONObject(jsonData);
    34.             //Returns the value mapped by the name if it exists and is a JSONArry
    35.             JSONArray jsonMainNode = jsonResponse.optJSONArray("Android");
    36.  
    37.             //Proccess the JSON node
    38.             int lenghtJsonArrar = jsonMainNode.length();
    39.             for (int i = 0; i<lenghtJsonArrar; i++)
    40.             {
    41.                 //Get object for each json node
    42.                 JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
    43.                 //Get the node values
    44.                 int song_id = Integer.parseInt(jsonChildNode.optString("song_id").toString());
    45.                 String song_name = jsonChildNode.optString("song_name").toString();
    46.                 String artist_name = jsonChildNode.optString("srtist_name").toString();
    47.  
    48.  
    49.                 //create the output
    50.                 outputData += "Node: \n\n " + song_id + " | "
    51.                                             + song_name + " | "
    52.                                             + artist_name + " \n\n ";
    53.  
    54.                 Log.d(TAG, outputData);
    55.             }
    56.  
    57.         } catch (JSONException e) {
    58.             e.printStackTrace();
    59.         }
    60.         return  outputData;
    61.     }
    62.  
    63. }
    64.  

    Thank you for any help with this I am at a loss of why it is not working.
     


    #1 HeavensSentSword, Jan 20, 2016
    Last edited: Jan 20, 2016
  2. Best Answer:
    Post #2 by Deleted User, Jan 20, 2016 (3 points)

    1. Download the Forums for Android™ app!


      Download

       
  3. Deleted User

    Deleted User Guest

    Well I can see a typo immediately

    Code (Text):
    1.  
    2.                 String artist_name = jsonChildNode.optString("srtist_name").toString();
    3.  
    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.
     
    HeavensSentSword likes this.
  4. HeavensSentSword

    HeavensSentSword Well-Known Member
    Thread Starter

    Thanks @LV426 . I am still getting use to andriod debugging. Ill give what you said a go. But, would just one misspelled word force the whole thing to stop working? Or would the parser just skip over it and ignore the error? This is my first Json attempt.
     
  5. Deleted User

    Deleted User Guest

    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.
     
    HeavensSentSword likes this.
Loading...

Share This Page

Loading...