English 中文(简体)
JSON JSON 转换
原标题:JSON conversion
  • 时间:2012-05-25 10:49:16
  •  标签:
  • android
  • json

我想知道如何将 http 响应从服务器转换为可解的 JSON 阵列

        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost postRequest = new HttpPost(
                    "http://riffre.com/chatapp/search.php?format=json");
            postRequest.setEntity(new UrlEncodedFormEntity(nameValuePair));
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            responsesrch = httpClient.execute(postRequest, responseHandler);

            Log.v("search response", responsesrch);

this is my code where I post a few parameters in form of name value pairs to the server and the value of responseserch that I get is

{"users":[{"user":{"city":"gurgaon","username":"zxc","userid":"6","gender":"Male","images":"0"}},{"user":{"city":"gurgaon","username":"tarun","userid":"5","gender":"Male","images":"0"}},{"user":{"city":"gurgaon","username":"vips","userid":"4","gender":"Male","images":"0"}},{"user":{"city":"gurgaon","username":"rah","userid":"3","gender":"Male","images":"0"}},{"user":{"city":"gurgaon","username":"aak","userid":"2","gender":"Male","images":"0"}}]}

这是用字符串格式, 所以我想知道我怎样才能把这个字符串转换成json阵列, 我不知道我必须如何使用输入流和所有这些实体, 任何帮助都会非常感激。

最佳回答

请看这个,很简单

http://www.androidhive.info/2012/01/andandroid-json-parsing-tutorive/

将 ISIS 转换为字符串

 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());
        }

将字符串转换为 Json 对象

try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

从对象获取矩阵

 // Getting Array of Contacts
    JSONArray  contacts = json.getJSONArray("users");
问题回答

您可以使用

  1. GSON
  2. JackSon

如果您想要使用json.org 的默认 Json, 那么请导入 org.json. Json. JsonArray; , 您可以做到 :

JSONArray jsonArray = new JSONArray(yourJsonString);

见:http://www.vogella.com/articles/AndroidJson/article.html

这是我工作守则的样本

   jArray = new JSONArray(result);
   JSONObject json_data = null;
   double[] tempLong = null;
   double[] tempLat = null;
   for (int i = 0; i < jArray.length(); i++) {
       json_data = jArray.getJSONObject(i);
       tempLong = new double[jArray.length()];
       tempLat = new double[jArray.length()];
       tempLong[i] = json_data.getDouble("longtitude");
       tempLat[i] = json_data.getDouble("latitude");
   }

我敢肯定,你可以调整它 适合你的需要

如果您想要将 Java 对象从/ 转换为 JSON, 您可以查看 : < a href="http://code.google.com/ p/google- gson/" rel=“ nofollow” >google- gson Java 库

有两条代码线

JSONObject jObj = new JSONObject(responsesrch.toString());
JSONArray jArr=jObj.getJSONArray("users");




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签