Actually, I have a JSONObject like this:
JSONObject json = new JSONObject();
json.put("type", "login");
json.put("friendList", FriendList);
and the FriendListis the type of ArrayList<String[]>
, then I use a socket to transfer JSON to my client.
My client received the data:
JSONObject receive_msg = new JSONObject(data);
String type = receive_msg.getString("type");
My question is how to get friendList with data typeArrayList<String[]>?
Thanks a lot if anyone helps.
you need to use JsonArray, iteratore over your list and fill the jsonArray then put it in the JsonObject
http://docs.oracle.com/javaee/7/api/javax/json/JsonArray.html
Related
im kinda new using JSON, and everything was fine until i have to make this JSON format.
"function":"ListarHoteles",
"parameters":[""]
Right now my code:
JSONObject JSONarr = new JSONObject();
JSONArray pam = new JSONArray();
jo.put("function", "ListarHoteles");
pam.add(" ");
JSONObject mainOBJ = new JSONObject();
mainOBJ.put("parameters", pam);
And im receiving:
{"{\"function\":\"ListarHoteles\"}":{},"parameters":[" "]}
Thank you
Is this what you want?
JSONObject jsonObject = new JSONObject();
jsonObject.put("function", "ListarHoteles");
JSONArray arr = new JSONArray();
arr.add("");
jsonObject.put("parameters", arr);
System.out.println(jsonObject.toString(2));
Output:
{
"function":"ListarHoteles",
"parameters":[
""
]
}
I am facing a problem to reach to an object which is inside an array of another object of another array. To make it simpler i am attaching the image (attached below), basically i want to reach to json object "0" inside jsonarray ticket itmes.
my codes are
JSONArray jsonArray = new JSONArray(buffer.toString());
JSONObject jsonobject = null;
jsonobject = jsonArray.getJSONObject(0);
JSONArray jsonarray1 = new JSONArray(buffer.toString());
jsonarray1 = jsonobject.getJSONArray("ticketitems");
JSONObject jsonobject1 = null;
TicketItem ticketItemList = new TicketItem();
jsonobject1 = jsonArray.getJSONObject(0);
ticketItemList.setItemCount(jsonobject1.getInt("itemCount"));
TicketItemList.add(ticketItemList);
However, jsonarray1 generates an exception stating
org.json.JSONException: No value for ticketitems
enter image description here
replace
jsonarray1 = jsonobject.getJSONArray("ticketitems");
with
jsonarray1 = jsonobject.getJSONArray("ticketItems");
It's case sensitive. It should be
jsonarray1 = jsonobject.getJSONArray("ticketItems");
Searching up on google I found this solution to read json strings from URL:
JSONObject json = new JSONObject(IOUtils.toString(new URL("https://somelink.com"), Charset.forName("UTF-8")));
The problem is that there is no JSONObject(String) constructor, why is that?
I thin you use google-gson, use org.json, there is constructor:
JSONObject obj = new JSONObject(str);
String myData = obj.getString("myData");
I'm new with JSON and my question might be not very hard, but I can not find the way to deal with my purpose.
I'm trying to develop the code so that I can manage some JSON content. In my case the JSON info is:
{"posts":[{"id":1a00b,"name":"Michael Thomson","info":"he is crazy"},
{"id":18,"name":"Jason Williams","info":"he is tall"}]}
Now, I'd like to get the strings from each JSON object (using Java). That's the code I have developed:
HttpResponse response = httpclient.execute(httppost);
String jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
JSONArray jsonArray = jsonResult.getJSONArray("posts");
for (int i = 0; i < jsonArray.length(); i++)
{
JSONObject childJSONObject = jsonArray.getJSONObject(i);
String id = childJSONObject.getString("id");
String name = childJSONObject.getString("name");
String info = childJSONObject.getString("info");
}
The error seems to be related with the sentence:
JSONArray jsonArray = jsonResult.getJSONArray("posts");
The method getJSONArray(String) is undefined for the type String
Those are the libraries I'm using to deal with
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONArray;
Thank you very much in advance!
jsonResult is a String. You need to turn it into a JSONObject first.
JSONObject obj = new JSONObject(jsonResult);
JSONArray jsonArray = obj.getJSONArray("posts");
I need my app to send an ArrayList<String[]> to php, I have this to call the service:
ArrayList<String[]> Items = new ArrayList<String[]>();
(...)
JSONObject JSONSend= new JSONObject();
JSONSend.put("Items", Items );
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000);
HttpResponse response;
HttpPost post = new HttpPost(SERVICE);
post.setHeader("json", JSONSend.toString());
StringEntity se = new StringEntity(JSONSend.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
post.setEntity(se);
response = client.execute(post);
and on the PHP service:
$data = file_get_contents('php://input');
$json = json_decode($data);
$Items = $json->{'Items'};
error_log($Items);
and the php is returning this:
[[Ljava.lang.String;#413e2fd0, [Ljava.lang.String;#413a2940, [Ljava.lang.String;#4139df18, [Ljava.lang.String;#4141b5b0, [Ljava.lang.String;#413931c8, [Ljava.lang.String;#41348b40, [Ljava.lang.String;#41393928]
The type of the $ServMade is string, so how can I take care of the data as an array on php? Am I missing something?
Try the following when generating json string in android:
JSONObject JSONSend= new JSONObject();
JSONArray ja = null;
JSONArray tmp = new JSONArray();
for(String s[] : Items){
ja = new JSONArray();
for(int i = 0; i<s.length; i++)
ja.put(s[i]);
tmp.put(ja);
}
JSONSend.put("Items", tmp);
The problem here is that You call .toString() (within Java) on an array of strings (String[]) that while NOT overrided returns exactly what You get in the JSON: [Ljava.lang.String;#413e2fd0].
Don't know exactly the JSONObject but I guess You should transform the ArrayList<String[]> (so the array of arrays of strings) to something different for JSONObject to handle it correctly.
Also, in PHP, You do not have to call $Items = $json->{'Items'}; - it is okay to call $Items = $json->Items;.