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.
And here is the JSON parser code .
Thank you for any help with this I am at a loss of why it is not working.
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: