1、Android從伺服器解析json,能解析成什麼樣的類型?比如。
話說simpleadapter構造裡面需要傳的也是個list集合吧?裡面可以嵌套map。返回的JSON數據是需要回看介面那邊的,根據接答口那邊的數據格式,來判斷是否需要list集合,或者實體對象。解析JSON兩種方法,百度下就有,另外,一般來說,解析Json後,用BaseAdapter的子類綁定數據會比較好。在一般的項目中,簡單適配器和數組適配器用的不多,大多處理數據的綁定問題都是繼承BaseAdapter的適配器。
2、android應用怎麼樣在伺服器端解析從客戶端發送過來的json數據
android中使用HTTP將數據發送到伺服器。HTTP可使用android自帶的httpclient框架實現。伺服器得到請求參數再做解析即可。以下是示例代碼:
1. GET 方式傳遞參數
//先將參數放入List,再對參數進行URL編碼
List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("param1", "數據")); //增加參數1
params.add(new BasicNameValuePair("param2", "value2"));//增加參數2
String param = URLEncodedUtils.format(params, "UTF-8");//對參數編碼
String baseUrl = "伺服器介面完整URL";
HttpGet getMethod = new HttpGet(baseUrl + "?" + param);//將URL與參數拼接
HttpClient httpClient = new DefaultHttpClient();
try {
HttpResponse response = httpClient.execute(getMethod); //發起GET請求
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //獲取響應碼
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8"));//獲取伺服器響應內容
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
2. POST方式 方式傳遞參數
//和GET方式一樣,先將參數放入List
params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("param1", "Post方法"));//增加參數1
params.add(new BasicNameValuePair("param2", "第二個參數"));//增加參數2
try {
HttpPost postMethod = new HttpPost(baseUrl);//創建一個post請求
postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); //將參數填入POST Entity中
HttpResponse response = httpClient.execute(postMethod); //執行POST方法
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //獲取響應碼
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8")); //獲取響應內容
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
3、C#伺服器端接收並解析android客戶端傳過來的json數據
//從復http request取得json字元串
string jsonString = Server.UrlDecode(Request["json"]);
//從http request取得id
int groupid = Convert.ToInt32(Request["groupid"]);
//用自製帶的序列化方法將其轉換成定義好的model類,具體的方法說明可以百度下
List<UserModel> listmodel = JsonConvert.DeserializeObject<List<UserModel>>(jsonString);
//進行保存操作
service.SaveAuthority(groupid, listmodel);
4、android客戶端怎麼解析從web伺服器返回的json字元串?
你拼接的時候有問題。把「\」轉義掉或者去掉,接收到的結果是{"age":23,"name":"hh","sex":"nan"},再用你自己的方法解析就沒問題了
5、請問android怎樣通過json數據從伺服器獲取圖片
android裡面,源通過json數據是不會直接返回圖片的,只會返回圖片的url地址。
步驟: 1,通過解析json數據,獲取到圖片的地址。
2,通過圖片的地址,再一次的請求網路(用非同步任務或者hangdler裡面請求網路:比如:
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
BufferedInputStream is = new BufferedInputStream(conn.getInputStream());
)
3 通過BitmapFactory.decodeStream(裡面的參數是一個位元組流),該方法返回的是一個bitmap ,直接用imageview.setimagebitmap()就能展示圖片了。
說明: 在BitmapFactory.decodeStream這里返回的bitmap可以做進一步的優化,比如二次采樣,把獲取的bitmap存sd卡等等。。
6、android 在伺服器端生成json格式數據,在客戶端怎麼解析
因為這次要從伺服器端得到Json數據,並且通過解析之後把解析後的數據顯示在Android客戶端中,首先部署伺服器端代碼(直接使用Jsp/Servlet):
構造的Json數據如下:
[{"name":"張三","address":"北京","age":20},{"name":"李四","address":"上海","age":30},{"name":"王五","address":"深圳","age":35}]
[一]伺服器端(Person.java省略):
①:數據構造JsonService.java
public class JsonService {
public static List<Person> getListPerson() {
List<Person> mLists = new ArrayList<Person>();
mLists.add(new Person("張三", "北京", 20));
mLists.add(new Person("李四", "上海", 30));
mLists.add(new Person("王五", "深圳", 35));
return mLists;
}
②:Servlet的代碼(包括構造Json數據,沒有使用Json數據轉換方法)JsonServlet.java
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
List<Person> persons = JsonService.getListPerson();
StringBuffer sb = new StringBuffer();
sb.append('[');
for (Person person : persons) {
sb.append('{').append("\"name\":").append("\""+person.getName()+"\"").append(","); sb.append("\"address\":").append("\""+person.getAddress()+"\"").append(",");
sb.append("\"age\":").append(person.getAge());
sb.append('}').append(",");
}
sb.deleteCharAt(sb.length() - 1);
sb.append(']');
out.write(new String(sb));
out.flush();
out.close();
}
③:部署到Tomact 瀏覽器輸入http://localhost/JsonWeb/JsonServlet直接訪問結果如下:
[{"name":"張三","address":"北京","age":20},{"name":"李四","address":"上海","age":30},{"name":"王五","address":"深圳","age":35}]
至此伺服器端代碼編碼完成,下面進行客戶端代碼編寫; (二)客戶端(Person類,和展示數據的布局文件因為簡單省去) ①:獲取伺服器端的Json數據並且解析的工具類JsonParse.java 必要的需要導入的包省去
public class JsonParse {
/**
* 解析Json數據
*
* @param urlPath
* @return mlists
* @throws Exception
*/
public static List<Person> getListPerson(String urlPath) throws Exception {
List<Person> mlists = new ArrayList<Person>();
byte[] data = readParse(urlPath);
JSONArray array = new JSONArray(new String(data));
for (int i = 0; i < array.length(); i++) {
JSONObject item = array.getJSONObject(i);
String name = item.getString("name");
String address = item.getString("address");
int age = item.getInt("age");
mlists.add(new Person(name, address, age));
}
return mlists;
}
/**
* 從指定的url中獲取位元組數組
*
* @param urlPath
* @return 位元組數組
* @throws Exception
*/
public static byte[] readParse(String urlPath) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len = 0;
URL url = new URL(urlPath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream inStream = conn.getInputStream();
while ((len = inStream.read(data)) != -1) {
outStream.write(data, 0, len);
}
inStream.close();
return outStream.toByteArray();
}
}
②:主Activity類
public class MainActivity extends Activity {
private Button mButton;
private ListView mListView;
//使用IP不能使用localhost或者127.0.0.1,因為android模擬器默認綁定這個IP,這里應該訪問區域網IP
private static final String urlPath = "http://10.16.31.207/JsonWeb/JsonServlet";
private static final String TAG = "MainActivity";
private List<Person> persons;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton = (Button) findViewById(R.id.button1);
mListView = (ListView) findViewById(R.id.listView1);
mButton.setOnClickListener(new MyOnClickListener());
}
private class MyOnClickListener implements OnClickListener {
@Override
public void onClick(View v) {
try {
// 得到Json解析成功之後數據
persons = JsonParse.getListPerson(urlPath);
List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < persons.size(); i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("name", persons.get(i).getName());
map.put("address", persons.get(i).getAddress());
map.put("age", persons.get(i).getAge());
data.add(map);
}
//初始化適配器,並且綁定數據
SimpleAdapter _Adapter = new SimpleAdapter(MainActivity.this,
data, R.layout.listview_item, new String[] { "name",
"address", "age" }, new int[] { R.id.textView1,
R.id.textView2, R.id.textView3 });
mListView.setAdapter(_Adapter);
} catch (Exception e) {
Toast.makeText(MainActivity.this, "解析失敗", 2000).show();
Log.i(TAG, e.toString());
}
}
}
7、Android Json解析伺服器文件
JSONObject aJosnObj;
aJosnObj = new JSONObject(stateJson);
JSONArray arrSpecialtys = aJosnObj.getJSONArray("Stations");
for (int i = 0; i < arrSpecialtys.length(); i++) {
JSONObject aJos = (JSONObject) arrSpecialtys.get(i);
String startingStation = aJos.getString("StartingStation");
String lat = aJos.getString("Lat");
}
String lineNum = aJos.getString("LineNum");
這樣就可以把"中國科技大學"、31.8308990633944和版24分別存放在權startingStation、lat和lineNum中
8、android Json解析 服務端返回的Json格式如下
String json="....";//裡面是你的json字元串
定義一個類用來存儲你得到的數據
把你 的userId loginname password usernaem 這些定義為user類的私內有屬性
並為容這些類定義set get 方法
class user{
private int UserID;
pubilc int getUSerID(){
return UserID:
}
public void setUserID(int userID){
this.userID=userID;
}
}
解析 json
JsonObject obj=new JsonObject(json).getJsonObject(「ResultUserLoginInfo」);
User user=new User();
user.setUserID(obj.getInt("UserID"); //這樣就對數據進行了 保存
純手敲 望採納!!!
9、android中使用JAVA解析json數據
這個格式使用如下代碼解析
try {
JSONObject jsonObject = new JSONObject(json);
JSONObject weatherinfo = jsonObject.getJSONObject("weatherinfo");
System.out.println(weatherinfo.getString("city"));
System.out.println(weatherinfo.getString("cityid"));
System.out.println(weatherinfo.getString("temp"));
System.out.println(weatherinfo.getString("WD"));
} catch (JSONException e) {
e.printStackTrace();
}
其中第一行代碼 JSONObject jsonObject = new JSONObject(json);//json 即為你的字元串
比如現在天氣信息是多個城市的,並非只有北京市。
{"weatherinfo":[{"city":"北京","cityid":"101010100","temp":"4","WD":"東風","WS":"2級","SD":"75%","WSE":"2","time":"10:45","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB","njd":"暫無實況","qy":"1011"},{"city":"天津","cityid":"101010100","temp":"4","WD":"東風","WS":"2級","SD":"75%","WSE":"2","time":"10:45","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB","njd":"暫無實況","qy":"1011"}]}
以下代碼適用。
try {
JSONObject jsonObject=new JSONObject(json);
JSONArray jsonArray=jsonObject.getJSONArray("weatherinfo");
for (int i=0;i<jsonArray.length();i++){
JSONObject object = jsonArray.getJSONObject(i);
System.out.println(object.getString("city"));
System.out.println(object.getString("cityid"));
System.out.println(object.getString("temp"));
System.out.println(object.getString("WD"));
}
} catch (JSONException e) {
e.printStackTrace();
}