0

I have a problem while creating jsonStringer in android. My problem is I have to post values to server using post method.So for that I have to send an array. { "name":"asdf","age":"42","HaveFiles":["abcfile","bedFile","cefFile"]} .

So how can I create a json array for haveFiles? And I don't know the no of files it may varies. So I am creating a string builder and appending the values to that. when I print the jsonString the stringbuilder show that instead of " it shows \". But when I print the string builder it looks ["abcFile"] like this. but in jsonStringer it prints ["\""abcFile\""]. How I can resolve this issue?

1

2 Answers 2

1

Use this to create json object and pass the json object

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

If you want jsonArray then

jobj.getJSONArray(TAG_NAME);

Sign up to request clarification or add additional context in comments.

Comments

0

It is really simple, you can use GSON library to do it.

The usage is something like:

Gson gson = new Gson();
String jsonStr = gson.toJson(yourObj);
YourObjType yourObj2 = gson.fromJson(jsonStr, YourObjType.class);

Regarding to your situation, you can do:

Gson gson = new Gson();
String[] ss = new String[] {"abcFile", "defFile", "ghiFile"};
String jsonStr = gson.toJson(ss);

And the result of jsonStr is:

["abcFile, defFile, ghiFile"]

10 Comments

What...? GSON is not for this purpose.
have to use json only.Otherwise can you send me an example
. If you are sending more information to server you library:jackson or gson
@Srikanth I am a little bit confused here, you are talking about you have problems when converting objects to json. But I have tried with GSON it is pretty good. What do you mean by "json only"? GSON is the best json serializer/deserializer on Android.
could you please create a sample for my object?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.