如何在String varaiables中存储json数据?

时间:2017-12-11 13:33:32

标签: java android json

如何在json个变量中存储String个数据。我尝试过但是显示错误。我希望将json保存在一个正常的string下面。我尝试了很多,但它始终显示错误。

[  
   {  
      "order":0,
      "name":"expenseType",
      "required":true,
      "type":"text",
      "placeholder":"Expense Type"
   },
   {  
      "order":1,
      "name":"date",
      "required":true,
      "type":"text",
      "placeholder":"Expense Date"
   },
   {  
      "order":3,
      "name":"amount",
      "required":true,
      "type":"number",
      "placeholder":"Enter Amount"
   },
   {  
      "order":4,
      "name":"description",
      "required":true,
      "type":"text",
      "placeholder":"Description"
   },
   {  
      "order":5,
      "name":"imageUrls",
      "type":"fileUpload",
      "options":[ urls:string]
   }
]
  

错误:org.json.JSONException:字符为641的未终止数组

2 个答案:

答案 0 :(得分:2)

在JsonObject上使用toString()。

String jsonString=jsonObject.toString();

从字符串中重写Json对象。

 JSONObject jsonObject=new JSONObject(jsonString);

如果您希望Json String为常量,则只需将其粘贴到双引号之间即可。示例json。

{    “令牌”: “”,    “类型”: “1”,    “信息”:{       “类型”:1,       “消息”:“消息”    }    }

和String一样。

  String jsonString ="{\n" +
           "   \"token\":\"\",\n" +
           "   \"type\":\"1\",\n" +
           "   \"message\":{\n" +
           "      \"type\":1,\n" +
           "      \"message\":\"message\"\n" +
           "   }\n" +
           "}";

答案 1 :(得分:0)

你可以使用GSON,创建你的对象类

Gradle:

compile 'com.google.code.gson:gson:2.8.2'

Java:

Gson gson = new Gson();
String myJson = "my string json array";
List<Object> list= gson.fromJson(myJson,new TypeToken<List<Object>>(){}.getType());

请参阅Documentation