Java android issuese上的简单JSON解析及其输出
我正在学习JSON,并且已经取得了很大的进步。我正确地解析了信息。我调试了它并查看了值,它们是正确的。问题是输出变量的节点为Null。我就是不明白这是为什么。我在这里有点迷路了。任何帮助都将不胜感激 所以我的主要活动课是这个Java android issuese上的简单JSON解析及其输出,java,android,json,parsing,Java,Android,Json,Parsing,我正在学习JSON,并且已经取得了很大的进步。我正确地解析了信息。我调试了它并查看了值,它们是正确的。问题是输出变量的节点为Null。我就是不明白这是为什么。我在这里有点迷路了。任何帮助都将不胜感激 所以我的主要活动课是这个 import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import andr
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);
}
});
}
}
我的JSON解析器是这样的
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: ";
String TAG1 = "Value for JSON";
//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("artist_name").toString();
//Debug Testing code
String songID = jsonChildNode.getString("song_id").toString();
Log.d(TAG1,songID );
String songName = jsonChildNode.getString("song_name").toString();
Log.d(TAG1,songName );
String artName = jsonChildNode.getString("artist_name").toString();
Log.d(TAG1,artName );
//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;
}
}
导入android.util.Log;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
/**
*由MMILLAR于2016年1月20日创建。
*JSON解析器类
*使用它来解析所有JSON数据
*/
公共类JSONParser{
JSONObject jsonResponse;
//字符串OutputData=“”;
//字符串jsonData=null;
String TAG=“JSON输出:”;
String TAG1=“JSON的值”;
//承包商
公共JSONParser()
{
//this.jsonData=jsonData;
//this.OutputData=OutputData;
}
公共字符串解析数据(字符串jsonData、字符串outputData)
{
尝试
{
//使用JSON字符串中的名称/值映射创建一个新的JSONObject
jsonResponse=新的JSONObject(jsonData);
//返回由名称映射的值(如果该值存在并且是JSONArry)
JSONArray jsonMainNode=jsonResponse.optJSONArray(“Android”);
//处理JSON节点
int lenghtJsonArrar=jsonMainNode.length();
对于(inti=0;i而言,问题似乎就在这里
//create the output
outputData += "Node: \n\n " + song_id + " | "
+ song_name + " | "
+ artist_name + " \n\n ";
您正在使用+=operator
,这意味着
outputData = outputData + "Node: \n\n " + song_id + " | "
+ song_name + " | "
+ artist_name + " \n\n ";
但是outputData的初始值为null,因此结果为null。您只需使用空字符串而不是null值初始化outputData变量
String outputData = "";
并将您的setOnClickListener()
更改为
/******** Listener for button click ********/
bparsejson.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "Button Pushed");
JSONParser jp = new JSONParser();
output.setText(jp.parsesData(strJson, outputData));
}
});
希望能有帮助!!问题似乎就在这里
//create the output
outputData += "Node: \n\n " + song_id + " | "
+ song_name + " | "
+ artist_name + " \n\n ";
您正在使用+=operator
,这意味着
outputData = outputData + "Node: \n\n " + song_id + " | "
+ song_name + " | "
+ artist_name + " \n\n ";
但是outputData的初始值为null,因此结果为null。您只需使用空字符串而不是null值初始化outputData变量
String outputData = "";
并将您的setOnClickListener()
更改为
/******** Listener for button click ********/
bparsejson.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "Button Pushed");
JSONParser jp = new JSONParser();
output.setText(jp.parsesData(strJson, outputData));
}
});
希望它能有所帮助。!!您需要使用从方法返回的值:
outputData = jp.parsesData(strJson, outputData);
现在你只是忽略它
此外,outputData
不能是final,否则您无法更改它。您需要使用从方法返回的值:
outputData = jp.parsesData(strJson, outputData);
现在你只是忽略它
此外,outputData
不能是final,否则您无法更改它。如果他将final字符串
传递给该方法并尝试在那里更改其值,是否也会有问题?@JustinPowell是的,谢谢。没有看到这一点。在方法中更改不是问题,因为参数不是final。这不是一个问题吗他将final字符串
传递给方法并试图在那里更改其值,这也会有问题吗?@JustinPowell是的,谢谢。没有看到。更改方法不是问题,因为参数不是final。是的,这很有效,我甚至没有考虑+=问题。非常感谢。是的,这很有效,我没有考虑我想一想这个问题。非常感谢。我很感激。