在android中转换特定类型的json字符串

Converting a particular type of json string in android

本文关键字:类型 json 字符串 android 转换      更新时间:2023-09-26

我有一个json格式的文件

var list=[{"Name":"arun","City":"xyx","CountryName":"KUWAIT"},"Name":"Amal","City":"abb","CountryName":"BAHRAIN"}];

从我的web服务器接收到我的android应用程序作为字符串值,我需要将其转换为适当的json格式。绳子的长度可以根据情况变大。我已经看到并转换json字符串和json数组只接收到这种格式:

{"Android":[{"slno":"1","name":"abc","number1":"123","number2":"456"},{"slno":"2","name":"def","number1":"789","number2":"000"},{"slno":"3","name":"","number1":"234","number2":"567"},{"slno":"4","name":"ghi","number1":"890","number2":"345"}]}

但我不知道如何转换在我的android应用程序收到的第一个json字符串。我如何在我的应用程序转换它。我如何转换它?

我不知道你所说的转换是什么意思。我假设你想解析json考虑

[ // JSONArray node
    {  // JSONObject node index 0
        "Name": "arun", // key value pair.
        "City": "xyx",
        "CountryName": "KUWAIT"
    },
    {                        // JSONObject node index 1
        "Name": "Amal",
        "City": "abb",
        "CountryName": "BAHRAIN"
    }
]
解析

JSONArray jr = new JSONArray("jsonstring");
for(int i=0;i<jr.length();i++)
{
     JSONObject jb = (JSONObject) jr.getJSONObject(i);
     String name = jb.getString("Name");
     String city = jb.getString("City");
     String cname = jb.getString("CountryName"); 
}

使用此代码进行HTTP请求(用于url中的url通道)

HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 500);
Client = new DefaultHttpClient(params);
httpget = new HttpGet(url);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
mContent = Client.execute(httpget, responseHandler);

然后使用这个来获取你的数据:

JSONObject jsonObject = new JSONObject(mContent);
String firstname = jsonObject.getString("firstName");
String id = jsonObject.getString("Name"");
String headline = jsonObject.getString("City");
String lastName = jsonObject.getString("CountryName");

然后放入asynctask


最终代码:

public class AsyncTaskActivity extends Activity implements OnClickListener {
        Button btn;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            btn = (Button) findViewById(R.id.button1);
            // because we implement OnClickListener we only have to pass "this"
            // (much easier)
            btn.setOnClickListener(this);
        }
        public void onClick(View view) {
            // detect the view that was "clicked"
            switch (view.getId()) {
            case R.id.button1:
                new LongOperation().execute("");
                break;
            }
        }
        private class LongOperation extends AsyncTask<String, Void, String> {
            @Override
            protected String doInBackground(String... params) {
                HttpParams params = new BasicHttpParams();
                HttpConnectionParams.setConnectionTimeout(params, 500);
               Client = new DefaultHttpClient(params);
               httpget = new HttpGet(url);
               ResponseHandler<String> responseHandler = new BasicResponseHandler();
               mContent = Client.execute(httpget, responseHandler);

              JSONObject jsonObject = new JSONObject(mContent);
              String firstname = jsonObject.getString("firstName");
              String id = jsonObject.getString("Name"");
              String headline = jsonObject.getString("City");
               String lastName = jsonObject.getString("CountryName");

                return "Executed";
            }
            @Override
            protected void onPostExecute(String result) {
                TextView txt = (TextView) findViewById(R.id.output);
                txt.setText("Executed"); // txt.setText(result);
                // might want to change "executed" for the returned string passed
                // into onPostExecute() but that is upto you
            }
            @Override
            protected void onPreExecute() {}
            @Override
            protected void onProgressUpdate(Void... values) {}
        }
    }

如果在调试中有问题请告诉我