How to store json data in String varaiables?

问题内容:

How to store json data in String variables. I tried but it is showing an error.I want to store below json in one normal string.I tried lot it’s always showing error.

[  
   {  
      "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]
   }
]

Error: org.json.JSONException: Unterminated array at character 641

问题评论:

    
What is the error it is throwing?
    
@ Pradeep Simha it’s showing compile error
– Mariyappan
2 hours ago
1  
share your java code
    
@YoLo just i want to store as string
– Mariyappan
2 hours ago
1  
You need to tell us what is the compile error you are getting and show us the relevant code 🙂

答案:

答案1:

Just use toString() on JsonObject.

String jsonString=jsonObject.toString();

Rcreate Json Object from String .

 JSONObject jsonObject=new JSONObject(jsonString);

If you want Json String as constant then just paste it beetween double quotes. Example json .

{
“token”:””,
“type”:”1″,
“message”:{
“type”:1,
“message”:”message”
}
}

And as String it is.

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

答案评论:

    
I donot what like this..
– Mariyappan
2 hours ago
    
Use updated answer.
    
Same thing i tried sir…i getting error Error: org.json.JSONException: Unterminated array at character 641
– Mariyappan
2 hours ago
    
Then your json is not valid i think . Check in online Json Viewer if you are using valid json or not

答案2:

you can use GSON , create your object class

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

See the Documentation

答案评论:

    
i did use any asytask…just i want to store as a string bro
– Mariyappan
2 hours ago
    
it is not an asytask , Gson is a library for Parse data, it make life very easy , Object -> Json or Json -> Object

原文地址:

https://stackoverflow.com/questions/47754123/how-to-store-json-data-in-string-varaiables

添加评论

友情链接:蝴蝶教程