我已经放了一个网址
private static String url = "http://10.50.101.27:8090/AndroidApp/downloads/result.json";
并尝试在android中使用json解析器进行解析,但它无法正常工作。
我的Json格式如下:
{"userdetails":[{"address":"Thrissur","name":"Pramoj","userid":"user001"},{"address":"Trivandrum","name":"Santhosh","userid":"user002"}]}
我该如何解析。 json解析没有响应。请帮帮我。
答案 0 :(得分:1)
试试这个:
JSONObject jObject = new JSONObject(response);
JSONArray jArray = jObject.getJSONArray("userdetails");
for (i = 0; i < jArray.length();i++){
JSONObject oneObject = jArray.getJSONObject(i);
oneObject.getString("address");//return the address
oneObject.getString("name");//return the name
oneObject.getString("userid");//return the userId
}
答案 1 :(得分:0)
首先将response string
从server
转换为 JSONObject since its structure is a JSONObject.
然后从JSONArray userdetails
获取JSONObject
}。通过该数组循环,并在每个位置获取JSONObject's
。然后获得strings(address,name,userid)
JSONObject.
示例代码段如下..
JSONObject obj = new JSONObject(response);
JSONArray jArr = obj.getJSONArray("userdetails");
for(int i=0; i < jArr.length(); i++)
{
JSONObject jObj = jArr.getJSONObject(i);
Log.v("address",jObj.getString("address").trim());
Log.v("name",jObj.getString("name").trim());
Log.v("userid",jObj.getString("userid").trim());
}
答案 2 :(得分:0)
public void parseJson(String jsonString)
{
try
{
JSONObject json = new JSONObject(jsonString);
JSONArray userdetails = json.getJSONArray("userdetails");
for (int i=0; i<userdetails.length(); i++)
{
JSONObject user = userdetails.getJSONObject(i);
String address = user.getString("address");
String name = user.getString("name");
String userid = user.getString("userid");
}
}
catch (Exception e) {}
}
答案 3 :(得分:0)
JSONParser类:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
解析代码:
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of userdetails
userdetails = json.getJSONArray("userdetails");
// looping through All userdetails
for(int i = 0; i < userdetails.length(); i++){
JSONObject c = userdetails.getJSONObject(i);
// Storing each json item in variable
String id = c.getString("userid");
String name = c.getString("name");
String address = c.getString(address);
}
} catch (JSONException e) {
e.printStackTrace();
答案 4 :(得分:0)
url =“你的json网址”
public class Uploadfilechecking extends AsyncTask<String, String, String> {
Activity mcontext;
String username, password;
boolean checkbox_value;
private ProgressDialog progressDialog;
private String JSONResp;
StringBuilder sb = new StringBuilder();
String Success_msg;
private JSONArray tmp;
private View progressView;
private Object fileUploadManager;
protected WeakReference<Context> context;
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try {
URL u = new URL(url);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setRequestMethod("GET");
conn.connect();
InputStream is = conn.getInputStream();
// Read the stream
byte[] b = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while (is.read(b) != -1)
baos.write(b);
JSONResp = new String(baos.toByteArray());
} catch (Throwable t) {
t.printStackTrace();
}
return JSONResp;
}
@Override
protected void onPostExecute(String result) {
System.out.println("filecheckresult" + result);
try {
JSONObject obj = new JSONObject(result);
JSONArray array=obj.getJSONArray("userdetails");
for (int i = 0; i < array.length(); i++) {
JSONObject actor = array.getJSONObject(i);
System.out.println(actor.getString("address"));
}
}catch(JSONException ex)
{
ex.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
答案 5 :(得分:0)
JSONObject jObj = new JSONObject(response);
JSONArray arr = jObj.getJSONArray("userdetails");
for (int i = 0; i < arr .length();i++){
JSONObject innerobject = arr .getJSONObject(i);
innerobject.getString("address");
innerobject.getString("name");
innerobject.getString("userid");
}
答案 6 :(得分:0)
为userdetails创建类和getter / setter
public class userdetails {
private String name, address, userid;
}
创建接收器类
public class Sink {
List<userdetails> userdetails;
}
将该响应称为接收
List<userdetails>user_details= new ArrayList<userdetails>();
SinkItem datas = new Gson().fromJson(response, SinkItem.class);
user_details= datas.userdetails;
将Gson库导入libs文件夹