以JSON格式存储字符串数据

时间:2013-10-04 06:30:41

标签: android json

 String Person_Name=et1.getText().toString();
  String Mobile_Number=et2.getText().toString();
  String Person_Query=et3.getText().toString();
  String Action=et4.getText().toString();
  String dis=Person_Name+Mobile_Number+Person_Query+Action;

这是我的所有数据收集..现在我想以JSON格式存储此数据...所以请告诉我答案。

3 个答案:

答案 0 :(得分:1)

String Person_Name; String Mobile_Number;String Person_Query; String Action; 
try
    {
        JSONObject action=new JSONObject();
        JSONObject user=new JSONObject();
        action.put("person_name", Person_Name);
        action.put("mobile_number", Mobile_Number);
        action.put("person_query", Person_Query);
        action.put("action", Action);

        JSONObject company=new JSONObject();
        user.put("company", company);
        user.put("userstatus", "Active");


        //.... 
    }
    catch (Exception je)
    {

    }

参考:http://snipplr.com/view/53225/

答案 1 :(得分:0)

try {
            JSONObject parent = new JSONObject();
            JSONObject jsonObject = new JSONObject();


            jsonObject.put("name",Person_Name);
            jsonObject.put("number", Mobile_Number);
            jsonObject.put("query",Person_Query);
            jsonObject.put("action",Action);
            parent.put("result", jsonObject);

        } catch (JSONException e) {
            e.printStackTrace();
        }

输出 -

{
       "result": {
         "name":"Person_Name",
        "number":"Mobile_Number",
        "query":"Person_Query",
        "action":"Action"
       }
     }

如果你想添加json数组:

    JSONArray jsonArray = new JSONArray();
        jsonArray.put("list1");
        jsonArray.put("list2");
        jsonObject.put("mylist", jsonArray);

   output:  {
     "mylist":[ "list1",
           "list2"]
            }

答案 2 :(得分:0)

这是如何以Json格式存储和检索

商品

JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("Person_Name", Person_Name);
        jsonObject.addProperty("Mobile_Number", Mobile_Number);
        jsonObject.addProperty("Person_Query", Person_Query);
        jsonObject.addProperty("Action", Action);

     **Retrieve**

jsonObject.get("Person_Name");
jsonObject.get("Mobile_Number");
jsonObject.get("Person_Query");
jsonObject.get("Action");