我正在尝试使用以下信息解析JSON,因为您可以看到它提供了一个具有值然后是JSON数组的JSON对象,如果排除了JSON对象(结果:1),我可以解析下面的内容但是,当我尝试下面的代码并按下按钮来检索数据时,它没有提供任何数据。
网址看起来与此相似,但有大量数据:
{
"result":1,
"data":[
{
"id":"1",
"langtitude":"31.3256632",
"latitude":"20.3256632",
"userNumber":"23",
"address":"adfsf",
"userFK":"1"
},
{
"id":"2",
"langtitude":"31.3256632",
"latitude":"20.3256632",
"userNumber":"23",
"address":"adfsf",
"userFK":"1"
}
]
}
MainActivity.Java
public class MainActivity extends AppCompatActivity {
private TextView tv_json;
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_json = (TextView)findViewById(R.id.tv_json);
Button btn_parsJSON = (Button)findViewById(R.id.btn_parsJSON);
btn_parsJSON.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new JSONTask().execute("I put the URL here");
}
});
}
public class JSONTask extends AsyncTask<String, String, String>{
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("Please wait ...");
progressDialog.setMessage("Grabbing Data");
progressDialog.setCancelable(false);
progressDialog.show();
super.onPreExecute();
}
@Override
protected String doInBackground(String... urls) {
{
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(urls[0]);
connection = (HttpURLConnection)url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while((line = reader.readLine()) != null) {
buffer.append(line);
}
String finalJson = buffer.toString();
JSONObject parentObject = new JSONObject(finalJson);
JSONObject array = parentObject.getJSONObject("result");
JSONArray parentArray = array.getJSONArray("data");
StringBuffer finalBuffer = new StringBuffer();
for (int i = 0; i < parentArray.length(); i++) {
JSONObject finalObject = parentArray.getJSONObject(i);
double langtitude = finalObject.getDouble("langtitude");
double latitude = finalObject.getDouble("latitude");
int userNumber = finalObject.getInt("userNumber");
String address = finalObject.getString("address");
int userFK = finalObject.getInt("userFK");
finalBuffer.append(langtitude + "\n" + latitude + "\n" + userNumber + "\n"
+ address + "\n" + userFK + "\n\n");
}
return finalBuffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
try {
if(reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
tv_json.setText(result);
progressDialog.dismiss();
}
}
}
答案 0 :(得分:0)
这可能会解决问题 - 请记住我没有尝试过,所以它可能包含错别字,但希望它会指出你有用的方向:
String response = //your json;
JsonObject jsonObject = new JsonObject(response);
JsonArray jsonArray = jsonObject.getjsonArray("data");
for(int i=0; i<jsonArray.length(); i++) {
JsonObject tobject = jsonArray.get(i);
}
答案 1 :(得分:0)
当你这样做时
JSONObject parentObject = new JSONObject(finalJson);
JSONObject array = parentObject.getJSONObject("result");
JSONArray parentArray = array.getJSONArray("data");
您正在结果块中寻找数据。你不会在那里找到它。我想你应该这样做:
JSONArray parentArray = parentObject.getJSONArray("data");
**编辑:**刚试了一下
String finalJson = "{ \n" +
"\"result\":1,\n" +
"\"data\":[ \n" +
"{ \n" +
"\"id\":\"1\",\n" +
"\"langtitude\":\"31.3256632\",\n" +
"\"latitude\":\"20.3256632\",\n" +
"\"userNumber\":\"23\",\n" +
"\"address\":\"adfsf\",\n" +
"\"userFK\":\"1\"\n" +
"},\n" +
"{ \n" +
"\"id\":\"2\",\n" +
"\"langtitude\":\"31.3256632\",\n" +
"\"latitude\":\"20.3256632\",\n" +
"\"userNumber\":\"23\",\n" +
"\"address\":\"adfsf\",\n" +
"\"userFK\":\"1\"\n" +
"}\n" +
"]\n" +
"}";
try {
JSONObject reader = new JSONObject(finalJson);
int result = reader.getInt("result");
JSONArray data = reader.getJSONArray("data");
Log.i("MYTAG", "reader:" + reader);
Log.i("MYTAG", "result: "+result);
Log.i("MYTAG", "data: "+data);
} catch (Exception e) {
e.printStackTrace();
}
日志:
读者:{&#34;导致&#34;:1,&#34;数据&#34;:[{&#34; ID&#34;:&#34; 1&#34;&#34; langtitude&#34;:&#34; 31.3256632&#34;&#34;纬度&#34;:&#34; 20.3256632&#34;&#34; userNumber&#34;:&#34; 23&#34 ;,&#34;地址&#34;:&#34; adfsf&#34;&#34; userFK&#34;:&#34; 1&#34;},{&#34; ID&#34;:& #34; 2&#34;&#34; langtitude&#34;:&#34; 31.3256632&#34;&#34;纬度&#34;:&#34; 20.3256632&#34;&#34; userNumber&#34;:&#34; 23&#34;&#34;地址&#34;:&#34; adfsf&#34;&#34; userFK&#34;:&#34; 1&#34;} ]}
结果:1
数据:[{&#34; ID&#34;:&#34; 1&#34;&#34; langtitude&#34;:&#34; 31.3256632&#34;&#34;纬度&# 34;:&#34; 20.3256632&#34;&#34; userNumber&#34;:&#34; 23&#34;&#34;地址&#34;:&#34; adfsf&#34;,& #34; userFK&#34;:&#34; 1&#34;},{&#34; ID&#34;:&#34; 2&#34;&#34; langtitude&#34;:&#34; 31.3256632&#34;&#34;纬度&#34;:&#34; 20.3256632&#34;&#34; userNumber&#34;:&#34; 23&#34;&#34;地址&#34 ;:&#34; adfsf&#34;&#34; userFK&#34;:&#34; 1&#34;}]